CS51 - Spring 2010 - Lecture 25
administrative
- if you're a senior and I haven't talked to you, let me know ASAP
- last CS lunch of the semester on Thursday at noon in Frank West
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
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
show
HTMLLinkFinderUsingSockets code
- does the same thing, but we've implemented the interaction in the HTTPConnection class
- notice it call getHost() on the url, then calls getPage
- what is the difference between a host and a page?
- why does this version of the URL constructor have two parameters?
show HTTPConnection class in
HTMLLinkFinderUsingSockets code
- constructor
- uses sockets
- we're the client
- open a socket to the host at port 80
- port 80 is the standard port for http, which is for web data
- need to open both input and output streams, why?
- true specifies autoflush, i.e. println() flushes the buffer
- getWebPage
- we have a connection to the server, but need to tell it what we want
- "GET pagename" tells the server we want the html for pagename
- web servers automatically disconnect after sending data. Why do you think this is?
- readResponse
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"