Interprocess communication: TCP sockets
Goals
- 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.
Credits
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 prior to the lab or in 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.
Set Up
Create a directory for today’s lab, ~/csci315/Labs/Lab4. Then 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 lab04.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.
Problem 1 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)
Problme 2 Read the code in echod.c and echoreq.c.
- Explain how one program addresses the other program for communication over a TCP socket. (4 points)
- Explain the purpose of the bind(2) call. (4 points)
- Explain the purpose of the connect(2) call. (4 points)
- Explain the purpose of the listen(2) call. (4 points)
- Explain the purpose of the accept(2) call. (4 points)
- 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)
Grading Rubric
Problem 1 [5 points total]
- [5 points] Explain the similarities and differences between pipe(2) and open(2) in a clear language. Write your answers in lab04.txt
Problem 2 [25 points total]
Answer the questions clearly in lab04.txt.
- [4 points] The TCP question;
- [4 points] The bind(2) question;
- [4 points] The connect(2) question;
- [4 points] The listen(2) question;
- [4 points] The accept(2) question.
- [5 points] Explain how you’d send and receive messages by reading echod.c and echo.c.
When you are done with this, you need to:
- cd ~/csci315/Labs/Lab4
- git pull
- git add lab04.txt
- git commit -m “Pre-lab 4 completed”
- git push