Pre-lab UDP

Interprocess communication: UDP sockets


  • Learn to work with UDP sockets: You have worked with pipes and TCP sockets for interprocess communication, both of which use a byte-stream model. In this lab, you will work with UDP, a model based on “datagrams,” that is, units of packaged data. You will see that while, in some sense, UDP sockets are easier to set up, they don’t offer the same guarantees of in-order and reliable delivery upon which you have been relying.
  • Practice with the concept of mutual exclusion: One of the advantages to using threads is that all your threads can share a common set of state variables (in this case, your server statistics). One difficulty arises, however, since mutual exclusion is needed in the access to shared variables by the concurrent threads of execution. If mutual exclusion is not used, your program is subject to race conditions and may not execute correctly.


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 UDP 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 on your own. You will have an introductory lesson on UDP sockets at the start of the lab session.

You can use at Stevens/Rago Ch. 16 (pp. 589-623), Beej’s Guide to Network Programming, or any other source you like.

Create a directory for this lab: ~/csci315/Labs/Lab6. Copy into it the following files:

  • ~cs315/Labs/Lab6/send_udp.c
  • ~cs315/Labs/Lab6/recv_udp.c
  • ~cs315/Labs/Lab6/Makefile

Problem 1

a) Study the code provided to you in programs send_udp.c and recv_udp.c, which can be built using the given Makefile. These programs are not terribly well commented: your task is to do your best to understand them and to write correct comments for the lines marked as ADD COMMENT HERE.

b) Two functions used in these programs are of particular interest to you: recvfrom and sendto. Read their man pages to understand what these functions do. Write in your file pre-lab.txt a brief explanation of what you learned about these functions from the man pages.

c) In pre-lab.txt, write a brief explanation about how different it is to work with UDP and TCP sockets. Use and cite your favorite sources.

d) Modify your programs and the Makefile to work with the wrappers.h and wrappers.c files you have been developing over the semester.  Augment your wrappers files to include the new calls used in the programs you were given in this lab. It is particularly important to do error checking for bind, because the port you want may already be in use by another process.

e) Change the value of the #defined constant MYPORT to something of your own, above 8000. Compile the programs using make and execute them both in your local host.  First, run recv_udp (note that this program contains an infinite loop, so you will want to put in the background with & so you can have your shell prompt back).

Next, run send_udp several times, passing localhost as command line parameter, and observe what happens. If you want to experiment a bit more, you can run each program on a different machines. In that case when you run send_udp, you should pass in the command line the name of the remote machine where you started recv_udp.

f) Finally, write the code for a new function called printsin() according to the specifications below, uncomment the calls to printsin in recv_udp.c and build the executable.

The function prototype must be:

void printsin(struct sockaddr_in *sin, char *pname, char* msg)

The function will print the information below in the sequence provided:

  • the string indicated by pname followed by a line break
  • the string indicated by msg followed by a space
  • the string “IP= ” followed by the IP address of the host associated with this socket address structure in dotted-decimal notation followed by a comma
  • the string “port= ” followed by the port number associated with this socket address structure and a line break

When this function is called, it should produce output as in the examples below:

function call: printsin(&s_in, “RECV_UDP:”, “Local socket is:”) produces output:

function call: printsin((struct sockaddr_in*)&from, “RECV_UDP: “, “Packet from:”) produces output:

When you are done, you need to:

  • cd ~/csci315/Labs/Lab6
  • git add pre-lab.txt
  • git add recv_udp.c
  • git add send_udp.c
  • git add wrappers.h
  • git add wrappers.c
  • git add Makefile
  • git commit -m “Pre-lab 6 completed”
  • git push
Print Friendly