Lab 2 - Basic TCP/IP Communication

Due: Next Friday

While we have not yet talked about the theoretical aspects of TCP/IP, this lab will let us explore some of the practical/coding aspects of an application that communicates over the network using the TCP/IP protocol. Many of the programs you use every day on the Internet use similar techniques to communicate.

You may work in groups of 1 to 3 students. Make sure to put ALL names in the comment section at the top of your submitted code so that all the lab partners get credit for the assignment. Only one lab partner needs to submit the code to me, but that person should CC the other lab partners so they too have a copy of the submitted code.

As discussed in the introduction, TCP is a connection-oriented transport protocol used for hosts to communication directly to one another and IP is the network layer protocol used to route data from the sender to the receiver. For Lab 1, we looked into the relationship between the IP address and domain name. In this lab, we will be compiling a small client application and a small server application that will communicate via TCP/IP.

A server is a special host that waits for data from other hosts and then performs some action based on that data. A server is associated with a specific IP address and port number. When you SSH to Odin for example, you are connecting to Odin's IP address (136.168.201.110) on the SSH port (port 22). On Odin, there is an SSH server process which waits for connection requests on port 22 and handles them.

A client is a host that is making requests of a server. The client initiates the connection by sending a connection request to the IP address and port number of the server. The server can then accept or reject the request. If the server accepts the request, the client can then send data to the server and the server can send data to the client. After the communication is complete, either the client or the server may close the connection.

This lab will show you how to use the following C/C++ library functions to send and receive data over the Internet (or any TCP/IP network).

These functions can be accessed by including the following header files on Odin (or other Linux systems):
       #include <sys/types.h>
       #include <sys/socket.h>
       #include <unistd.h>
You can get further information about each of these functions by looking at the man page for the function, such as "man 2 bind". Make sure to specify "man 2" to select the manual pages associated with the network libraries and not some other manual page. For example, "man accept" will give you the man page for the printer command "accept", while "man 2 accept" will give you the man page for the accept function.

The Sample Programs

You will need to be logged onto Odin or some other POSIX system with a command line compiler (e.g. Linux or Mac OS X). If you do not use Odin, your system will also need to support the make utility.

Log in to Odin and create a new directory for this lab assignment. Change to that directory and then download the tarball containing all the lab code using the following Odin command:

wget http://www.cs.csub.edu/~eddie/cmps3620/code/lab2/lab2_files.tgz
This tarball contains all the code that will be used for this lab. When extracted, all files will be put in your current directory, so be sure to be in the appropriate directory before using the following command to extract the files:
tar -xvzf lab2_files.tgz
Once you have extracted the files, type "make tcp" to compile the two programs for this lab: vcrec and vcsend.

These programs do simple TCP sends and receives. The programs do the following: vcrec calls socket() to get a socket descriptor, bind() to bind the socket, getsockname() to get the port number, listen() to wait for a connection and accept() to accept a connection. vcsend calls socket() then calls connect() to try to connect to vcrec. Once connected they are connected to each other, they use send() and recv() to communicate. When the communication is complete, they use close() to end the connection.

To run the programs, first start vcrec using the following command:

./vcrec [optional_buffer_size] 
This will print out the TCP port number it was able to bind for listening. Pass that port number to vcsend:
./vcsend <hostname> <portnumber> 
The "hostname" field is the hostname you are using for vcrec. If you are running vcsend on the same host as vcrec, you can use the hostname "localhost", which is a special hostname to indicate that the server is on the same host as the client.

Play around with these programs sending various strings back and forth. To close a connection, type just a period by itself in vcsend. This will close both programs. Rerun vcrec using the option to set the buffer size (e.g. "./vcrec 5 &"). Use large and then small values for the buffer size. Notice what happens when you use small values for the buffer size.

Assignment

Note that these programs are using simplex (one-way) communication; vcrec reads from the socket and vcsend writes the user's input to the socket. Modify the code to allow half-duplex (simple two-way) communication by having the following interaction between the two programs:
  1. vcsend asks the user for a string and sends it to vcrec
  2. vcrec echos the received string on screen
  3. vcrec asks the user for a string and sends it to vcsend
  4. vcsend echos the received string on screen
  5. Repeat steps 1-4 until the user enters a period by itself in either vcsend (step 1) or vcrec (step 3). When the user enters a period, close the connection and exit both vcrec and vcsend
Email your modified vcrec.c and vcsend.c to my account on Odin.