Lab 2 CS Majors

UDP Programming in C++

Lab Objective:

  • To gain experience using Unix/Linux UDP socket programming. You will be writting a UDP peer-to-peer chat program. This program will NOT have a server, it will make connections directly between two clients. Upon running the program, it will "search" for partners, and list them for the user. If no partners are available, it will continue to search until one (or more) is found. Once a partner is selected, the program will then allow the two clients to type messages to each other. There are enhancemens beyond this which you may choose.

Reading/Reference: Lab overview:

You will create programs using UDP to perform general chat sessions. You can use the code in /home/class/CSC4093/UDBChat/ and /home/class/CSC4093/BroadcastUDP/ as a starting point, but do not simply hack the code to achieve functionality. I will expect you to create a set of useful, reusable, network functions using sockets, and then to create well designed, structured code to achieve the required functionality.

Below first is the simple code from the first lab. This project is client/server oriented. You program will be peer-to-peer, that is you will only write ONE program, and run multiple instances that program to talk to each other.

The user will NOT enter IP addresses or port numbers - this aspect is hidden from the user. Rather each program will start up and broadcast packets announcing it's presence on a single port you choose (something above 5000). It will also listen on the same port, and build a list of partners it discovers. The user can than select a partner, or accept a request from another partner, and enter into a two way chat with that partner. If one partner terminates the chat by typing $exit, both sides will resume search for chat partners.

A tricky aspect of this project is that once you start waiting for a packet on a port, the program waits until it gets a packet, blocked. There is a solution - threads. The idea of threads is that the program is running multiple routine at the same time. The idea is that you can have a thread broadcasting beacons, another listening for beacon, one waiting for packets from a chat partner(s), and one waiting for user input. You can start all these threads, and than have them communicate through shared data structures in the program. Be careful, however, of race conditions.

Sample code for UDP Programming on Linux: (in "/home/class/csc4093/UDPChat")

Sample code for broadcasting: (in "/home/class/csc4093/UDPChat") Sample code for creating threads: (in "/home/class/csc4093/ThreadExample") Sample code for sending and receiving broadcasts in multiple threads (in "/home/class/csc4093/ThreadBroadcast") Thread Synchronization

The threads library provides three synchronization mechanisms:

  • mutexes - Mutual exclusion lock: Block access to variables by other threads. This enforces exclusive access by a thread to a variable or set of variables.
  • joins - Make a thread wait till others are complete (terminated).
  • condition variables - data type pthread_cond_t
Sample code for sending and receiving broadcasts in multiple threads with mutex for synchronization (in "/home/class/csc4093/ThreadBroadcastSync")

Lab tasks:

The lab is worth 100 points total. Below I give you a set of requirements and options. You may do as many or few – this is a chance to get extra credit. However, no matter what you do, you will not get higher then a 100 if you do not have good clean code. Your score is dependent on the highest level of CS course you have taken.

In general, if you want to do more than one thing at a time, threads are required, this is asynchronous interaction. The more simple approach is synchronous, where only one thig happens at a time, and one side must wait for the other.

Feature Required CS IISorted ascending Soft Dev Op Sys
Well commented code yes 5 5 5
On startup ask user for name to broadcast yes 5 5 5
Well Designed/Written Code yes 20 20 20
Transmit at least one beacon on startup (synchronous) yes 20 15 10
Wait for a partner beacon - Show name and allow user to connect and chat, or wait for next beacon (synchronous) yes 20 15 10
Display "Typing" if other user typing not with next 20 15 15
Character at a time transmission/reception (see characters as typed not with prev 20 15 15
Synchronous two-way chat function (take turns typing) Must do this or next 30 25 15
Display new possible partners as they appear (asynchronous)   30 20 15
N-way chat. Allow used to bring in multiple partners in a group chat   35 30 25
Asynchronous two-way chat function (Either side can type any time, asynchronous, requires threads) Must do this or previous 50 40 30
MAXIMUM SCORE   185 160 135

Turn in

  1. A document listing the features you support
  2. Screen captures clearly (with headings) demonstrating each feature support above.
  3. All source code (including makefile)
-- JimSkon - 2011-09-15
Topic revision: r6 - 2013-09-23 - JimSkon
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback