CS 051 Fall 2011

Lecture 36

Sockets

Our previous URL example returned the contents of a web page. The URL object hid
a lot of the details from us. To understand these details, we look at an example that does the same thing
using a Socket.

If we want to communicate with a machine over a network, we need to be able to send as well as
receive data. Sockets allow us to do this.

A Socket is like a phone connection. You can think of a phone connection as a bundle of two
separate connections. The first takes data from the mouthpiece of your phone, and sends it to
the earpiece of the other phone. The second connection takes data from the mouthpiece of the
othere phone, and sends it to your earpiece.

A Socket is similarly composed of two streams. The client writes on one stream, which the server
reads from. And the server writes on the other stream, which the client reads from. In our telnet
example, the server writes to a stream with the results of the commands that we sent it on the
other stream.

To use a Java Socket, we need to create a connection, then get the two Streams that we describe
above. Here is sample code that does these basic steps:

   Socket tcpSocket = new Socket("www.cs.pomona.edu", 80);

   BufferedReader input =
     new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));
   PrintWriter output =
     new PrintWriter(tcpSocket.getOutputStream(), true);

Note that a PrintWriter is similar to a FileWriter, except that FileWriter
can only write Strings or arrays of characters. PrintWriter can output all
types, just like System.out.println().

The boolean value true that we send to the PrintWriter constructor tells the
PrintWriter that is should flush the output stream after calls to println, printf,
or format.

Now that the connection is established, we can request a web page by writing the GET command on
the output stream. For example:

   output.println("GET " + pageName);

To get my home page, we would set pageName to "/~kcoogan/index.html"

The server's response to our command will be written on the server's output stream, which is the
client's input stream. We can get it in the client the same way we read any input stream:

   String response = "";
   String curline = input.readLine();
   while(curline != null) {
     response = response + "\n" + curline;
     curline = input.readLine();
   }

   return response;

Drawing Panel Network

The class example DrawingPanelNetwork allowed us to open two instances of
the program, and send objects back and forth between them. This technique used Sockets,
and works equally well when the program instances are not on the same machine. You will be
writing a very similar program for your next lab.

Other Protocols

There are many other protocols besides http. For example, POP stands for Post Office Protocol. It
is used to access email accounts, and uses port 110 by default.

Here are the commands provided bye a POP server:

USER username
This command tells the POP server which user’s mail to read. Instead of username you would identify
the account to log into. This command responds with a single line which starts with the string +OK.

PASS password
This command tells the POP server the password corresponding to the previous user account. Instead
of password you would provide the user’s actual password. This command responds with a single line.
If the line begins with +OK the login worked. If the login failed, it returns a line beginning with
-ERR.

The remaining commands will only work after a successful login.

STAT
This returns some simple statistics about the mailbox. The line it returns has the following form:
+OK 3 496
The first number reports the number of messages in the mailbox. The second reports the total
number of characters in all the messages.

TOP 1 0
This returns the header of the message followed by some number of lines. The first number identifies
the message number to return. The second number indicates how many lines of the message body to
return. As shown above, the command would return only the header for the 1st message.
This message returns first with a line that begins either with +OK or -ERR. It the line it returns
begins with +OK, it then sends multiple lines that are the header and the number of body lines
requested. You can tell when it is done sending lines because the last line will consist of a single
period character (.).

RETR 1
This command is a lot like TOP except that it returns the entire message requested, both header and
body, in their entirety. The number you provide is the message number.
This command returns first with a line that begins either with +OK or -ERR. It the line it returns
begins with +OK, it then sends multiple lines that are the header and complete body of the mail
message requested. You can tell when there it is done sending lines because the last line will
consist of a single period character (.).

QUIT
This command ends the connection with the mail server. It always returns with a single line
beginning with +OK.

You can try out this commands using telnet to connect to a mail server on port 110. Just type in
the commands shown above and see the responses that you get back.
Unfortunately, not many
mail servers support unencrypted POP connections anymore, so you may have trouble finding one
to try out.