Lab 2 - Basic TCP/IP Communication
The lab is worth 10 points.
Due: Friday January 18, 2008 at 5:00pm
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 a program
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.
This lab will show you how to use the following networking functions to send
and receive data over the Internet (or any TCP/IP network):
- socket() - Create a byte stream that will be used to send and receive data.
- bind() - Bind the socket to a particular address (IP address and port number).
- getsockname() - Find out what address a socket is bound to. Useful if bind() asks the system to assign a port number.
- listen() - Wait for a connection to the bound address. Done by the server.
- connect() - Request a connection to a specific address. This is done on the client's side to initiate a connection-oriented channel with the server.
- accept() - Accept a connection request to the bound address. This establishes the connection-oriented channel on the server's side.
- send() - Send data across the communication channel.
- recv() - Receive data from the communication channel.
- close() - Terminate the communication. This ends the connection-oriented channel. Any further communications must first use connect/accept to establish a new channel.
You can get further information about each of these functions by looking at
the man page for the function.
The Sample Programs
We will be using the code provided by Dr. Marc Thomas as the basis of this
lab. You will need to be logged onto Helios or some other system with a
command line compiler (e.g. Linux or Mac OS X). Create a new directory for
this lab assignment. Then go to
http://www.cs.csub.edu/~marc/code/cs376.html
and download the following files to your lab directory:
- vcsend.c
- vcrec.c
- makefile.unx (makefile.vc5 if on a Windows system)
- header.h
- startup.h
- startup.c
- cleanup.c
- diagnost.c
- diagnost.h
On Helios, you can also copy these directly from the directory
/usr/users/marc/public_html/code/cs376/
.
Rename makefile.unx to Makefile. Type "make tcp". If you have a command line
compiler and make utility installed (like gcc and GNU make), this should
compile all the helper utilities (startup, cleanup, etc) and the two program
binaries: vcrec and vcsend.
These binaries do simple TCP send and receive. 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() (closesocket() on Windows) 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 host [portnumber] [&]
The "host" field is the hostname you are using for vcrec (helios.cs.csubak.edu
if you are using Helios for example). If you are running this on your home
machine instead of Helios, then localhost should work for the hostname. If it
does not, try the IP address your machine is currently using. The "portnumber"
field is the port number printed out by vcrec when you started it. You can now
type information to send back and forth. To terminate the connection, start a
line with a period.
Play around with these programs sending various strings back and forth, then
continue on to the assignment section, which will have you make some
modifications to the programs and see how it affects the programs.
Assignment
Writeup the answers to these questions and then email them to my Helios account.
- Rerun vcrec using the option to set the buffer size (e.g. "vcrec 5 &"). Use
large and then small values for the buffer size. What is the effect of using
small values for the buffer size?
- Note that these programs are using simplex communication; vcrec reads
and vcsend writes.
- Make a modification that allows half-duplex communication (both can send
and receive, but not at the same time). What modification did you have to make
to allow half-duplex communication? Submit the code with this modification
as an attachment to this writeup.
- Can the code be modified to allow full-duplex communication (sending and
receiving at the same time)? Explain why or why not.