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
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:
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.
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.
The entries beginning with a * are services waiting for a client to connect. Other entries beginning with the machine name (defender) represent servers which are connected and currently providing a service.
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
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.
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.
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)
Good Luck!