CSE342 - Lab 7: UDP P2P Chat

Purpose: To explore a networked application not based on the client-server paradigm.

All of the network applications we've studied have been based on the client-server paradigm. Today, we'll investigate a network application in which all the participants are peers, with no designated "master" server.

The network application is a version of "chat". You'll write a program that accepts keyboard input, and sends the input to all other participants. Your program will also accept messages from other copies of the program, and display those messages on standard output.

Each message will consist of a character string (length determined by the length of the received message). The messages will be sent via UDP to the local broadcast address (all 1s), using port 4321.

EXERCISES:

  1. Write a receiver program that listens to UDP port 4321, and echos any input it receives to standard output. The program should bind its input socket to IP address INADDR_ANY and port 4321. Each line of output should begin with the IP address of the source of the message, followed by the message itself. Note that mixing printf and write(1, ..., ...) won't work very well because of UNIX buffering. You'll want to use one or the other method exclusively. The program should receive and echo input in an infinite loop.

    I've set up a test transmitter program that periodically broadcasts a message to port 4321. Your receiver should be able to pick up this broadcast (as well as any other messages sent by other students' transmitters).

  2. Write a transmitter program that reads from the keyboard and sends a copy of that input in a UDP broadcast message to port 4321. You should be able to test your transmitter against your receiver (UDP broadcast messages should be received at the originating host as well as by other hosts on the same subnet). Your transmitter should continue to accept keyboard input until the user enters EOF (^D), which is indicated by a return value of 0 to the read from the keyboard.

    If your transmitter returns an error message to the effect of "address already in use", you already have a copy of your program bound to port 4321. Use the ps command to locate that process and kill to terminate it. This condition will be more likely to occur in the next exercise.

  3. Combine your two programs into one program that uses fork() to start a separate receiver process. The UNIX fork() system call starts a second process that is an exact copy of the first process (executing the same code, copies of all local and global variables). The only difference is that fork() returns the process id of the new process to the old process, and returns 0 to the new process. The C idiom for using fork() looks like:
           if ((pid = fork()) == 0) {
               /* code for new process (receiver) goes here */
           }
           /* code for old process (transmitter) goes here */
    
           kill(pid, 9);    /* terminates other process */
           

    Be sure to terminate the receiver process from within the transmitter process after the user has entered ^D.

When complete: Demonstrate the correct functionality of your program.


Last revised 4 October 2009.