Computer Communications Fall 1998
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.
gcc -o udpcli udpcli.c
gcc -o udpserv udpserv.c
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
Turning in your answers:
Prepare an email answering the above questions. Mail the homework to firstname.lastname@example.org 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 6789If you do not have such a subject line, I will bounce your letter back to you and ask for a resubmission of the homework.