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.
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 Sleipnir for example, you are connecting to Sleipnir's IP address (136.168.201.100) on the SSH port (port 22). On Sleipnir, 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).
#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.
Log in to Sleipnir 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 Sleipnir command:
wget http://www.cs.csubak.edu/~melissa/cs376-s11/lab2_files.tgzThis 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.tgzOnce 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.