CMPSCI 453/591
Computer
Networking
Professor
Jim Kurose
Programming Assignment
1
Assigned: Class 7 (10/4/05)
Due: Class 10 (10/13/05)
Off campus: Postmarked ten days after viewing lecture
6or receiving this (whichever is later)
Last Modified: never (at least not yet)
In this programming assignment you will program the simple message relay application
shown below.
The sender process accepts user-entered "messages" from the keyboard,
and sends these messages to the receiver process via the message-relay process.
The receiver process displays a message once the message has been received in
its entirety. All communication among the processes is done using TCP sockets.There
are two application-level messages defined: a DATA message (containing the text
of a message) and a CLOSE message, which instructs the receiver of the CLOSE
message to shut down, as discussed below
- Sender process. The sender process accepts user-entered "messages"
(e.g., containing characters entered by the user via keyboard) and sends each
message to the relay and then waits for another user-entered message. You
can use whatever convention you want for user to indicate that an input message
is complete. Note that because of the btye-stream semantics of the TCP socket,
your sender process will need to define a application-layer DATA message that
lets the relay process know how many characters are in the incoming byte stream.
When the user indicates that there are no more messages to send (use whatever
covention you want to allow the user to indicate this as well), the sender
process should send a CLOSE message to the intermediate relay, and then terminate
itself gracefully (i.e., releasing any sockets it has been using).
- Intermediate-relay process. After establishing a connection
with the sender and receiver processes, this process reads messages sent by
the sender process. It forwards DATA messages to the receiver process over
a TCP socket.When the intermediate relay receives a CLOSE message from the
sender process, it should send a CLOSE message to the receiver process, and
then gracefully terminate itself.
- Receiver process. This process reads mesages sent by the intermediate-relay
process. It displays the content of each DATA message once that message has
been received in its entirety. On receipt of a CLOSE message, the receiver
process should terminate itself gracefully.
Since the goal of the programming assignment is for you to learn socket programming
(not to build a hardened application), you can hardwire in specific IP addresses
where the processes are known to be running (i.e., you need not somehow ask
the user for the host names or IP addresses of the process to which messages
will be sent). You may also assume that the relay and the receiver will only
be receiving messages from a single sender or relay, respectivly. The extra
credit option (i.e., not required) for this assignment asks you to program
a relay that can handle multiple senders concurrently.
Please read the information posted on the class WWW site about what to hand
with your assignment. Note that a short design document is required.Your should
program your each process to print an informative statement whenever it takes
an action (e.g., sends or receives a message, detects termination of input,
etc), so that you can see that your processes are working correctly (or not!).
Tis also allows the TA to also determine from this output if your processes
are working correctly. You should hand in screen shots (or file content, if
your process is writing to a file) of these informative messages.
You may write your programs in Java, C++, or C. Other languages are OK, but
ask me first. You are welcome to do this assignment using the WINSOCK API, rather
than the Unix API, if you have a Windoze machine. See http://www.lowtek.com/sockets/
for links to tutorials on the WINSOCK API.
Programming the assignment in JAVA
- Recall from our discussions in class that Java encapsulates the concept
of a client-side connection-oriented (TCP) socket with the class, Socket.
Details about the socket class, and the other classes noted below, can be
found here.
- Java encapsulates the concept of a server-side connection-oriented socket
with the class, ServerSocket. You'll need to use the accept()
and close(0) methods of this class, which are also document at
this URL.
- If you're interested in a quick Java tutorial targeted specifically at socket
programming, check out "Socket Programming in Java: a tutorial," by Q. Mahmoud,
Javaworld, Dec. 1996, http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets_p.html.
Sun's socket tutorial is at http://java.sun.com/docs/books/tutorial/networking/sockets/index.html.
Programming the assignment in C
If you choose to write your programs in C, you'll need to master the C system
calls needed for socket programming. These include socket(), bind(),
listen(), accept(), connect(), and close(). For an audio/html tutorial
of socket programming in C, see "Unix Network Programming," by Jim Kurose at
http://www-aml.cs.umass.edu/%7Ejsmanic/unetpgm/.
(You need only sessions 2, 3 and 4 to do this assignment; enter any username).
For a quick text-only tutorial of socket programming specifically under Linux,
see http://www.lowtek.com/sockets/.
Programming notes
Here are a few tips/thoughts to help you with the assignment:
- You must chose a server port number greater than 1023 (to be safe, choose
a server port number larger than 5000).
- I would strongly suggest that everyone begin by writing the sender and
relay processes first, i.e., just getting the two of them to interoperate
corrections. Then modify the relay and program the receiver.
- You will need to know your machine's IP address, when one process
connects to another. You can telnet to your own machine and seeing the
dotted decimal address displayed by the telnet program. You can also use the
UNIX nslookup command or you can ask your local system administrator for the
info. Or, under Windows, see the WINIPCFG utility.
- Many of you will be running the sender, relay, and receiver on the same
UNIX machine (e.g., by starting up the receiver and running it in the background,
then starting up the relay in the background, and then starting up the sender.
This is fine; since your using sockets for communication these processes can
run on the same machine or different machines without modificaiton. Recall
the use of the ampersand to start a process in the background. If you need
to kill a process after you have started it, you can use the UNIX kill
command. Use the UNIX ps command to find the process id of your
server
- Make sure you close every socket that you use in your program. If you abort
your program, the socket may still hang around and the next time you try and
bind a new socket to the port ID you previously used (but never closed), you
may get an error. Also, please be aware that port ID's, when bound
to sockets, are system-wide values and thus other students may be using the
port number you are trying to use.
Extra Credit.
OK. You've got your sender, relay, and receiver processes running.
You want to do more. For extra credit, rewrite your relay to be able to
handle multiple senders concurrently. Your relay still only needs to forward
messages to a single receiver (the goal here is to let your program a concurrent
server). The relay waits on the welcoming socket and then creates a new thread
or process to handle all communication with the incoming sender. You'll need
to do some extra reading on your own to figure out how to do this. See the resources
listed above.