CS337 Sockets lab

The purpose of this lab is to guide you through the process of compiling and running some very simple client/server software.

I. "Hello, world", client/server style

  1. Boot your machine in Linux.

  2. Get copies of tcpUtilities.h, tcpUtilities.cpp, helloClient.cpp, helloServer.cpp, and helloServerConcurrent.cpp into your account.

  3. Open two terminal windows.

  4. Compile and helloServer with the command

    g++ -o helloServer helloServer.cpp tcpUtilities.cpp

    Then start helloServer running on port 1234 like so:

    helloServer 1234 "hi there"

    The "hi there" will be the message sent by helloServer to any client that contacts it.

  5. Compile helloClient in your other terminal window using the command

    g++ -o helloClient helloClient.cpp tcpUtilities.cpp

    Then run helloClient like this:

    helloClient yourmachine.mathcs.carleton.edu 1234

    where "yourmachine" is the name of the computer you are working on. If all goes well, you will receive the "hi there" message in the client's terminal window, and a log message will appear in the server's window.

  6. Shut down the server with a CTRL-C in the server's terminal window. Now try running the server and client again, with only one difference. When you launch the server, do it with the following command:

    helloServer 1234 "hi there" &

    What's the difference? Do the messages appear as before? What does the ampersand do for you?

    To shut down the server, you have several choices. You can execute the command "fg" and then hit CTRL-C. Or you can look at the process ID that was given to you when you ran the helloServer command. Say the process ID was 5456. To kill the helloServer process, you could use the command "kill -9 5456". That's a couple ways, anyway.

    It is important to kill your servers once you are done with them. When you use the ampersand to run a process in the background, that process continues to run even when you log off. If you're not sure whether you have stray servers running, try the command "ps -a" to get a list of your processes and their IDs.

  7. Now team up with somebody using a different computer. Launch a hello server with different messages and ports on each machine, and then try using helloClient to get the message from one machine to the other.

II. The down-side of sequential servers

  1. Edit helloServer.cpp and make it send a thousand or two thousand or ten thousand hello messages whenever a client contacts it. Pick a number of messages that causes a noticeable delay (at least two or three seconds) from the time you execute the client to the time the response is complete.

  2. Once your server is causing a suitably long delay, launch helloServer in one terminal window and open two other terminals. Type the command "helloClient yourmachine.mathcs.carleton.edu 1234" in each of these terminal windows, but don't hit return right away. Get the two helloClient commands ready to execute, and then try to execute them as close together as possible. If this works correctly, whichever client connects to the server first will get its entire response before the server starts responding to the second client.

  3. Shut off helloServer and compile helloServerConcurrent.cpp (same compilation as above, including tcpUtilities.cpp).

  4. Try the same two-client trick with this new server. Does the second client have to wait for the first client to get its entire message? What do you think is going on?

III. Digging into the code

  1. Open helloServer.cpp and try to figure out how it works. You will probably find it helpful to read tcpUtilities.cpp. Use UNIX manual pages (e.g. "man accept"), too.

  2. Try to change helloServer.cpp so that it will only send the message after it receives the letter A from the client. You'll need to change helloClient.cpp to make it send an A, of course.

  3. Modify helloServerConcurrent.cpp so that its log message includes its process ID, which you can obtain using the function getpid (check those manual pages again).

  4. Think up your own experiments, or just start trying to understand these small programs line by line. We'll certainly talk about the details of the code in class Friday, but you will be better served by that discussion if you try to decipher the code yourself first.

    BSD Sockets: A Quick and Dirty Primer, by Jim Frost, is a pretty good tutorial that you might find useful.