Computer Networks

COS 461, Princeton University Spring 2015

Assignment 0: Introduction to Socket Programming

Due 11:59 p.m. Thursday, February 12, 2015

This assignment introduces you to basic socket programming as discussed in Precept 1. The goal is to get you comfortable with (a) Unix socket programming and (b) simple client-server interaction.

You are required to submit code, which compiles and runs on the FC 010 machines. Both programs should be written in C or C++.

Grading: This assignment will count towards your participation grade.

Note: You will be able to find copies of this program on many places on the web, as well as in some form in the textbook. We strongly recommend that you implement this program without consulting other references, as you will gain a much better understanding of these system calls, which you will heavily use in Assignment 1.

We want you to write a server capable of receiving text messages from clients over TCP sockets. The server should print these text messages on the standard output. From the server perspective, the message corresponds to the data received from a particular client during a communication session with that client. The server should be listening for text messages on a port known to the clients. It should handle the client connections sequentially and accept connections from multiple clients. After servicing one client to completion, it should proceed to the next. If multiple clients try to simultaneously send text messages to the server, the server should handle them one at a time (in any order). Serving one client at a time is enough.

We also want you to write a client. The client should read from a file -- either one named "filein.txt" that can be hardcoded in your source code, or from stdin -- then transmit the message and exit.

You can assume that the client is run as

  $ ./client server-IP-address port-number
where "server-IP-address" is the IP address of the server, and "port-number" is the TCP port the server listens on. The server is run as "server port-number". If the server cannot bind on a port, print a message to standard error.

The FC Lab machines firewalls low ports, so you should only use a port greater than 10000 and less than 60000 while testing.

You should use fread and fwrite calls for reading/writing data to and from sockets and files. Do not use special "string" versions of these calls (e.g., fgets and fputs as they are not designed for binary data).

Make sure you handle the following correctly:

  1. Local and remote operation: Your program should be able to operate when connection over both localhost (127.0.0.1) or to between machines. When testing, make sure you use an appropriate port to avoid firewall in the lab cluster. You can get the machine's routing IP address via ifconfig (in Linux/Mac) or your GUI Network Preferences. Note online websites tools like http://whatismyipaddress.com/ may not work because your machine may be behind a NAT and use a different IP address for wide-area communication than from communication between the client and server in the local cluster.

  2. Buffer management: Assume that the file contents can be arbitrarily large (assume a typical size of 20KB), but the buffers you use to read/write to the file or socket must be small and of fixed size (e.g., 4096 bytes).

  3. Handling return values: By default, sockets are blocking, and for this assignment we will use only blocking sockets. "Blocking" means that when we issue a socket call that cannot be done immediately (including not being able to read or write), our process is waits until it can perform the action. This includes the case when
    • a socket's internal buffer is full and therefore, no data can be written, or
    • a socket's buffer is empty, and no data is available to be read.

However, if there is some data available to be read or some can be written, the call will return the number of bytes read or written respectively. NOTE: This returned value can be less than specified in the length argument to the call or indicate an error. You must handle this.