Computer Science 335

Socket Programming Lab


Purpose

The purpose of this activity is to provide some practical experience in running and programming a socket-based client server application.An example already exists on-line which you can compile and run.Once that has been done, a series of simple programming changes can provide a basis of understanding how to create a simple client server application. In this lab you will

  1. explore some of the background retails of the connection
  2. run the sample application
  3. make some changes to the application to create a different client-server

Background

Sockets are a programming technique used to let an application on one machine talk to an application on another machine. One of the programs (the server) is designed to provide a service to another(the client). Server applications are started first and in many cases are actually started when the machine is booted. They wait for clients to connect and ask for help.

In order for a client to contact a server, the client must know the server's address. A server's address has two main parts:

  1. a name (america3) or an ip address (137.155.2.20)
  2. a port number (an integer..5000)

An example connection might be viewed as follows:

******************                       *****************
*     server     *                       *    client     *
*                *                       *               *
*  137.155.2.20  *   <--------------->   *  132.17.2.5   *
*                *                       *               *
*     5000       *                       *     12121     *
*                *                       *               *
******************                       *****************

When programming a server, the server code decides what port it will run on and the machine is obvious because that's a function of where you run it. The client must be programmed to know both the server machine and port number. The client port is actually determined randomly and is not a part of any of the code. Since the client connects to the server and not vice versa, the client machine name does not appear explicitly anywhere in the code either.

Netstat

There is a unix command which can be used to see the ports which are already in use. The name of the command is "netstat". Running netstat as "netstat -a" will show the list of all sockets in a format that is a little busy.


An Example

First read but do not run the socket example currently on-line. Do NOT spend a lot of time looking at the details of the code with the exception of the few instances cited below. This example is already set to have the server run on america3 on port 1234. In the example explanation, a session is illustrated where the server AND client are launched from america3

Copy the client and server into your Hunter Creech unix account along with the readdir file. The three files you will need can be copied from your browser if you know how to do that (the explanation of the example shows you how) or it can be copied from my user account using "cp". The necessary files are

The account directory is ~dgame/www/sockets/.

An explanation of how to compile the client and the server is provided in the link above(the program is in c not c++). See if you can compile it using the correct commands, but don't run it yet.

Before running it, let's consider some of the problems you might encounter.

If many students try to run on the same port, conflicts will occur when they use the same server port on the same machine. This is easy to avoid by having each of you choose some random number for the port. Choose a number between 3000 and 9000. If you receive a message that says the port is busy when you run the server, just choose another number/port. In this application, if you run the server it will halt once it serves a single client, and needs to be restarted before another client can access it. If you try to run the server a second time within a few minutes of the previous run it is possible that you will get a port busy message. If so, either change the port number and recompile or just wait a minute and run it again. This problem is a "hanging up" problem in the program and will go away by itself in a few minutes.

It is important to be able to change the application to run the server on another machine. In order to do that, the client code needs to be changed on the one line which identifies the host. Note that the server could run on any solaris machine without making changes, but the client has to know where the server is running so that it can properly access the service.This change can be done by modifying the HOST value in the client code.

So, in summary what we need to do to be able to run the server on another machine is change the following items in the code

  1. server port in the server code
  2. server name in the client code
  3. server port in the client code

NOW try to RECOMPILE both the client and server for the new port number and the name of the machine on which you intend to run the server. You will likely run the server on the machine that you have logged into although almost any machine could be used. The department has a policy against using america3, so be sure that you use some machine other than america3. And FINALLY..., RUN THEM. Try running the server in background as indicated in the example.

Once you understand what the server does and how to run it, move on to the next step.

Try running the client on a different machine. This would be more representative of the use of this type of application. If you are not aware of the names of the other machines in the lab, you can find out by either running "rup" in unix or more simply looking to your left or right if you are sitting in the lab.

  1. Launch the server
  2. Telnet to one of the other machines in the lab
    It would probably be best to open another terminal window from which the client can be run before using telnet. In this manner you can see the termination of the server in the original window after it finishes providing the service.
  3. Run the client as you did before.

Next let's change the client and server to do something different (but simpler). This will focus your attention on the parts of the code which you would change to write your own service.

  1. REQUIRED. Make another copy of the client and server and change them so that the client does not send anything but merely receives what the server sends and displays it. This can be done easily by commenting out the send command. Change the server to simply send your name in response to a connection. As in the client, comment out the recv command in the server. This server will always respond with your name. You will NOT need the readdir routine for the server in this activity. Completion of this step should only require changing a couple of lines of code. USE PORT NUMBER 5050.
  2. OPTIONAL. Try modifying that program to create a simple chat program. In order to do that, code the client to have a loop which will
        Read from the keyboard
        Send to the server
        Read from the server
        Display on the screen
    

    In order to do this activity, you will need to do a little research to be able to read and write with "printf" and "scanf" in c. If you do this part, it will become apparent to you when running your chat program that while it works, it is a little awkward to use. The timing problems of a client server program demand a different approach to work well and the approach necessary is beyond the scope of this lab.

You may be required to submit the results of this activity with the server program which responds with your name. Before submitting it, include in the server code a statement to output ("printf") the port number which you used when writing your code so that I can properly test it. Use the Directions for Submitting Programming Assignments to submit. Only submit the COMPILED(EXECUTABLE) server file.


Assignment Number: 0

Part: 1

File Name for server: server (the executable, to run on port 5050)

(use lower case for the name of the program file)


A client to use for testing the server can be found at ~dgame/www/sockets/cs335testlab
To run and test it type
cs335testlab servermachinename
e.g.
cs335testlab defender

Good Luck!


If you are interested in exploring C++ sockets, there is a functional example by Rob Tougher from the Linux Gazette in my ~dgame/www/sockets/socketsC++ directory including sources, header files and Makefile. You're welcome to try this one if you would like. The author has created a Socket class which functions like c++ streams. Good Luck.