Homework 4: 22C:178 & 055:134

Computer Communications Fall 1998

Assignment 4

[due 7 October]

The following programs are simple examples of how UDP can be used in a Unix environment. These programs are not examples of good programming -- they purposely have been oversimplified and most comments have been removed. They are initial examples that you can modify and experiment with to learn some network programming techniques. Some of the programming techniques will be explained in class.

The program udpcli.c is the ``client'' of the example. This program reads from standard input (the terminal) and sends lines in UDP datagrams to a server. You should be able to compile this program and test it with the corresponding server program, udpserv.c in the same machine (it uses localhost as the network address). To give an idea of just what the various system calls are for these two programs, you can see annotated versions of udpcli and udpserv as well in an HTML format. To experiment with these programs, follow these steps.

Copy the source files to your local directory.
Compile them with the commands
gcc -o udpcli udpcli.c
gcc -o udpserv udpserv.c
Then open an X-window and start the server program in that window with the command udpserv. It should print a message telling you it is waiting.
In another window, start the client program with the command udpcli. It should be waiting for your keyboad input. Enter some data, such as hello server. The data you enter should be printed in the server window.
After the above steps work, you should try some simple tests such as starting the client first, killing the server in the middle of a conversation with the client, and so on to see what happens if there is a failure. Another thing to try is to change localhost to some other machine name, such as horse@cs.uiowa.edu, and test the pair of UDP programs.

Recall that RPC (Remote Procedure Call) uses UDP to communicate between client and server. This means, of course, that a client expects some kind of answer returned from a request it sends the server. But the udpcli and udpserv examples above do not illustrate how the client gets a response from the server. This is slightly more complicated, because the server needs to send a datagram back to the client -- and how does the server know which port to use? The server's port is known to the client (in these example programs it is 5057), but the client's port must be determined by the server during runtime.

The trick is to use a feature not described in class: the server uses the recvfrom call, which identifies the client's address and port number (which is actually a ephemeral port number). The server sends its reply to the client using the address and port number returned by the recvfrom call. Thus, unlike the solution dicussed in class -- where each program is both a server and a client -- the following programs use another technique based on the ephemeral port.

Programs udpcli2.c and udpserv2.c illustrate the server responding to the client. The server receives text from the client, increments all the characters in that text (for instance, aaa becomes bbb) and sends the modified text back to the client. Instructions for compiling and testing these two programs are the same as for the first pair of UDP programs.

The Homework Questions

What happens if you have the client send a few lines to the server, then kill the server program (say by control-C) and try to send another line? Can you kill the server and restart it without the client knowing the difference?
Is it possible to change udpserv2.c so that it sends two messages back to the client with two sendto calls consecutively, while the udpcli2.c program is modified to read two times consecutively? Will this work?

Turning in your answers:

Prepare an email answering the above questions. Mail the homework to herman@cs.uiowa.edu and specify in the subject line, the course number, the assignment number and the last four digits of your student ID number (we are hoping that four digits will be unique). So, for example if your student number is 123456789, then the subject line of your email should be:

178 homework 4, student 6789
If you do not have such a subject line, I will bounce your letter back to you and ask for a resubmission of the homework.