CSE 422 Lab 2: Creating a Multi-Threaded Chat
In this lab you will be making a server application for a chat room.
You will need to use threading to
listen for a client’s message, as well as wait for any number of clients to connect.
We will give an
introduction of the Pthread library for this lab.
Along with threading, you will also have to use a mutex
lock to maintain stability of the list of clients. We have supplied a code skeleton for the server
application, including the locking and unlocking of the mutex.
We have also supplied the client application source code for you to use, but you should not alter that
code, because you will not be turning it in; your server will need to work with the client, as is.
application created will be named
, which takes as arguments the server hostname and a UDP port,
as in the client program in Lab 1.
The server is name
and takes no command line
You will need to download: lab2_server.cc, lab2_client.cc, lab2_packet.h, and the makefile.
will compile, but the server will not do anything useful.
Overview of the Chat Room Application:
The application will be able to allow multiple clients to connect at one time.
It will maintain a list of
the clients connected at any time.
Messages sent by a client will be received by the server and
broadcasted to all the clients.
Clients are allowed to come and go as they wish, and so the server must
be able to handle the clients leaving properly.
Clients are also able to check who is online at any time.
If the server is called to shutdown, it will inform the clients before closing it's connection and
terminating the application.
Pthread Library Introduction:
Here is a man page for pthread class: http://opengroup.org/onlinepubs/007908799/xsh/pthread.h.html
The four functions that you will have to use are: pthread_create(), pthread_detach(),
You will also have to use the pthread_t and
pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void *);
The first parameter must be a non null pointer to a pthread_t object.
The second is a set of attributes
to be used and for this project you should use NULL to set the default values.
The last two parameters
are the function to be called followed by the argument to be passed to the function.
If you notice they
are void parameters, so you will need to use type casting to pass values to your threaded function.
Also, if you want to pass more than one parameter to the threaded function, you should create a struct