Pre-lab 4

Interprocess communication: TCP sockets


  • Learn to work with TCP sockets. By now, you have learned about a few communication mechanisms for processes and threads.
    • When you have processes with a common ancestor (therefore, all running on the same machine), you can have them communicate with pipes.
    • When you have processes that don’t share a common ancestor, but still reside in the same machine, you can use files to exchange data. (You could also use shared memory or named pipes.)
  • When your processes are in different machines, however, you need something else: a form of communication that leverages an existing communication network. This lab will introduce you to TCP sockets, a mechanism to interconnect processes on different machines via internetworking.


The material developed for this lab was developed by Prof. L. Felipe Perrone. Permission to reuse this material in parts or in its entirety is granted provided that this “credits” note is not removed. Additional students files associated with this lab, as well as any existing solutions can be provided upon request by e- mail to: perrone[at]bucknell[dot]edu

Read about TCP sockets in your favorite source. The primary goal of this pre-lab is to develop your self-reliance and independent learning skills. Do the best you can to learn the material in advance. You will have an introductory lesson on TCP sockets at the start of the lab session.

Your goal should be to gain enough knowledge to understand the source code given to you, where two processes establish sockets through which they exchange messages. You can look at Stevens/Rago Ch. 16 (pp. 589-623), Beej’s Guide to Network Programming, or any other source you like.

Create a directory for todays lab, ~/csci315/Labs/Lab4. Copy into it the following files:

  • ~cs315/Labs/Lab4/echod.c
  • ~cs315/Labs/Lab4/echoreq.c

These files contain, respectively, the skeletons for an echo server and an echo client. The server is daemon called echod and runs on an arbitrary machine; it receives strings from clients through a TCP socket. The daemon sends back to the client echoreq the same string it received; the client displays the string on its standard output. Read through these files carefully, so that you can understand how the sockets are set up and verify that this is being done correctly.

Create a pre-lab.txt file, in which you will write answers for this assignment. Label your answers clearly with the name of the questions given to you below.

(P1) Read the manual page for the socket(2) call. Compare this call to pipe(2) and open(2); explain the similarities and the differences you observe. (5 points)

(P2) Read the code in echod.c and echoreq.c.

  1. Explain how one program addresses the other program for communication over a TCP socket. (4 points)
  2. Explain the purpose of the bind(2) call. (4 points)
  3. Explain the purpose of the connect(2) call. (4 points)
  4. Explain the purpose of the listen(2) call. (4 points)
  5. Explain the purpose of the accept(2) call. (4 points)
  6. One of your tasks in lab will be to implement message exchanges between echoreq and echod over a TCP socket. Do a little reading to discover what system calls you will need to use. Explain how you will be able to send and to receive messages. (5 points)

When you are done with this, you need to:

  • cd ~/csci315/Labs/Lab4
  • git add pre-lab.txt
  • git commit -m “Pre-lab 4 completed”
  • git push
Print Friendly