CS51 - Spring 2010 - Lecture 24
quiz
sending/receiving data externally
- What type of data am I sending?
- characters/strings
- use Readers and Writers
- built-in types
- use DataInputStream/DataOutputStream
- objects and or a mixtures of types
- use ObjectInputStream/ObjectOutputStream
- Buffered or unbuffered?
- Unbuffered data sends immediately
- Buffered data queues it up and then sends it in larger chunks
- Often we'll want to buffer our data, but sometimes immediate action is necessary
- For reader/writers
- BufferedReader
- PrintWriter
- For streams
- BufferedInputStream
- BufferedOutputStream
- All of the above take as a parameter to the constructor another Stream or Reader/Writer. So, to get things started:
- If you're reading/writing from/to a file:
- Characters/strings
- FileReader/FileWriter
- Stream
- FileInputStream
- FileOutputStream
- If you're reading from the network or some other device, there will frequently be a getInputStream() or similar method.
look at
DrawingPanelNetwork code
- Sockets
- a socket is a framework for bidirectional communication
- sockets are specified by an IP address and a port
- every computer has an IP address, think of it like a phone number
- consists of 4 numbers 0 to 255 xxx.xxx.xxx.xxx
- or, you can think of it as 4 bytes
- think of the port, like an extension, it uniquely identifies what program/process on a computer another program is talking to
- Looking up ip addresses
- "nslookup <domain_name>"
- nslookup www.cs.pomona.edu
- the server only need to specify a port, which is the port it is listening on
- "localhost", is a keyword for specifying the current computer
- beAServer()
- ServerSocket is used to wait for a connection from a client
- note that we only need to specify the port
- drawingSocket.accept()
- another blocking operation
- waits for the client to connect and gives us the socket for the connection
- socket.getOutputStream()
- uses writeToStream to transmit the data
- beAClient()
- creates a new socket using the ip and the port specified
- socket.getInputStream()
- can leverage readFromStream
Pictionary lab
- Encouraged to work in partners
- Two key challenges:
- Understanding what is going on with the code provided. Spend time looking at the code and walking through the execution.
- I/O and Stream processing
- If you read the lab beforehand and have thought about it a bit, most people will finish in lab (I want you to be working on your test programs...)
show
SimpleIM demo
- only need client/server relationship to know who should contact who
- how does this work for most IM clients?
what needs in happen in IM program?
- establish a connection (should look similar to DrawingPanelNetwork)
- from these we get a socket (multidirectional)
- both the client and the server now should do the same thing
- get reader/writers for both input and output
- if we have text to write, send it out
- if we receive text, display it
- what type of I/O classes should we use (Streams or Reader/Writer)?
show
SimpleIM code
- SimpleIM.actionPerformed
- what are the actions?
- notice that we use the same class for both the server and the client
- when the user enters text, we set the text for the message handler
- IMMessageHandler
- again, random port
-
http://en.wikipedia.org/wiki/TCP_and_UDP_port
-
http://www.iana.org/assignments/port-numbers
- after establishing a connection, they do the same thing
- use BufferedReader and PrintWriter because we're dealing with Strings/characters
- also give us the ability to send a line at a time
- use InputStreamReader to go from a stream to a reader
- ready() returns true if there is data to read, false otherwise
- see if we have text to send, if so, send it
- just keep looping
Look at the mystery method in Mystery class
- what does it do?
show
HTMLLinkFinderBufferedReader demo
show
HTMLLinkFinderBufferedReader code
- getWebPage method
- creates a new URL object
- URL = uniform resource locator
- url.openStream()
- read the data using a BufferedReader
- new String(buildpage), could also have used builldpage.toString()
- throws MalformedURLException
telnet
- telnet is a command line program with similar functionality to HTTPConnection
- "telnet <host_name> <port>" will open a connection
- standard web server port is 80
- "telnet www.google.edu 80"
- to fetch a web page once you have a connection open:
GET <page_name> HTTP/1.1
- a common page is "/index.html"