CPSC 441, Fall 2002
Lab 3: Socket Programming
IN THIS LAB, you will start to write TCP/IP applications using the Sockets interface. You can work in either Java or in C++. Information on programming with Sockets in Java can be found in the textbook. If you want to use C++, you should use the Socket and ServerSocket classes that I wrote for you. These can be found in the directory /home/cs441/Sockets, along with two sample applications. You can copy this directory into your homework directory (or a subdirectory of that directory) with commands such as:
cd homework cp -r /home/cs441/Sockets .I suggest that you try the sample chat application. (I handed out a copy of the source code in class.) You can use it to talk to someone else in the class, or you can run it in two windows on a single machine. Note how the program requires strict alternation between sending and receiving. You might want to try connecting to a domain name that doesn't exist, to see what kind of error you get. You could also try connecting to a computer that is not running the server.
If you write a program that uses the C++ Socket classes, don't forget to add #include "Sockets.h" at the beginning of the program. Compile the program using a command of the form
g++ myProg.cc Sockets.o or, better, g++ -Wall -o myProg myProg.cc Sockets.o(The file Sockets.o was made by compiling Sockets.cc. You could use Sockets.cc directly in the g++ command, but then it is recompiled every time you run the compilation.)
Remember that in C++, variables can hold either pointers or objects. If you use a pointer variable to refer to an object, you need the "->" operator to call methods in the object. For example: listener->accept(). On the other hand, with an object variable you would use the "." operator. For example: listnener.accept().
The exercises for this lab are due next Friday. You should turn in printouts of your programs and put the programs in your homework directory so that I can run them if necessary.
Exercise 1: The "daytime" service is a very simple server that runs on some computers. When a client contacts this server, it sends one line of text that contains the current time on the server in human-readable format. The server then closes the connection. The daytime service runs on port 13. It is running on the computer named eck.hws.edu.
Write a daytime client program that connects to eck.hws.edu on port 13, reads a string from the server, and displays the string to the user. With no error checking or comments, the main() routine only has to be two or three lines long. For full credit, add a try/catch statement to handle possible errors and report an error message if an error occurs. You should, of course, add comments to the program in accordance with the usual rules of programming style.
Exercise 2: Write a "quote of the day" (QOTD) server and client. You will be writing two separate programs, one to act as the server and one to act as the client. A QOTD server makes available a pithy quotation. When a client connects to the server, the server will send the quotation to the client and then close the connection. The quotation can be several lines long, and can contain blank lines. The quotation is to be stored in a file on the server computer. An administrator can change the quotation by editing this file. It is not necessary to modify the server program. Other than that, you can decide on the exact interaction between client and server. In a separate file, the server makes a list of the IP address of each computer that connects to the server.
The server must be able to handle multiple clients, one after another. It should try to do this robustly. That is, if an error occurs during communication with one client, it should not crash the server.
David Eck, 20 September 2002