CS51 - Spring 2010 - Lecture 24
sending/receiving data externally
- What type of data am I sending?
- 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
- For streams
- 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:
- If you're reading from the network or some other device, there will frequently be a getInputStream() or similar method.
- 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
- ServerSocket is used to wait for a connection from a client
- note that we only need to specify the port
- another blocking operation
- waits for the client to connect and gives us the socket for the connection
- uses writeToStream to transmit the data
- creates a new socket using the ip and the port specified
- can leverage readFromStream
- 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...)
- 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)?
- 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
- again, random port
- 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?
- getWebPage method
- creates a new URL object
- URL = uniform resource locator
- read the data using a BufferedReader
- new String(buildpage), could also have used builldpage.toString()
- throws MalformedURLException
- 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"