chat- Network Chat Client
In this lab, you will:
This is a group assignment.
server <IP address> <port number>
client <IP address> <port number>
The goal of this assignment is to write a networked chat server and client. Multiple users will connect concurrently to a single server process via individual chat clients. The client program will send messages from users to the server, and will print messages from the server to the user's terminal screen. The server will keep track of connected clients, and when it recieves a message from any client it will pass that message along to all other connected clients. Each user will also have a name that is used to identify their statements.
Each user, when they first connect, should get a default name
consisting of the string "User" followed by a unique set of digits.
When a user connects to the server any other connected users should
recieve a message stating "
<user name> has connected".
Additionally, the client will support two
name command: When the client enters the phrase "
followed by a space and at least one character, everything after the space
will become the user's name. The system will then send this name in front of
any subsequent chat messages. If the user specifies another name their current
name is replaced. Lastly, when any
user changes their name then all other users should recieve the message
<old name> has changed their name to <new name>"
quit command: When the client enters the phrase "
the client program will terminate. All other connected users should recieve
the message "<user name> has quit".
Messages should always be printed to all terminals in less than a second, regardless of the current state of the system. (This means that your client should be able to display text even while waiting for input, and your server should be able to respond to an incoming request by any client at any time.)
If a client suddenly finds its socket to be closed then it should quit and print a message saying the server shut down unexpectedly. If the server finds a socket unable to be used it should print the same message as though the user quit normally.
127.0.0.1(the loopback address) will cause all traffic to stay local. Using this IP you can run your client and your server on the same machine. I won't require you to test your code across the internet, but it should work in theory. (Things like firewalls can make this harder than it it first seems. I'm happy to give advice on how to get this running if you'd like.)
socket(). In fact,
accept()returns a different descriptor for each individual connection.
pthreadto wait for server messages and print them to the screen when they arrive. Don't worry about incoming messages messing up the display of half-written messages.
mallocthis time around.
read()sockets or calls to
accept()new connections. You can set sockets to be nonblocking. With a nonblocking socket calls to
read()will fail and return an error if there is nothing to read. Similarly, calls to
accept()will fail and return an error if there are no new connections to accept. In both cases the function returns an error value and sets
errnoto be one of
accept4()man page and the
socket()man page for the phrase "
SOCK_NONBLOCK" for details on how to make sockets nonblocking. You will want to set the socket returned from
socket()as nonblocking, as well as the sockets returned from
accept()(for this second one, use the
sprintf()to format strings (char buffers) easily. For example, if you have a
namestring and a
messagestring, and you want to combine these into a
toSendstring, you can use something like:
sprintf(toSend, "%s: %s", name, message);
If you find yourself wanting to use
quitwhen it is appropriate. You shouldn't let clients set their names to be empty. The client shouldn't quit on the input "quite right!"
connect()only once in your client code. In particular, don't call
connect()every time you send a message. The connection that is established can be used over and over.
Create a .tgz archive of your lab directory and email it to
Your submission must contain a file
which contains definitions for
calloc but has no
and with all output (
The simple syntax for creating a .tgz archive is as follows:
tar -zvcf new_archive.tgz lab_directory
The syntax for unpacking a .tgz archive is:
tar -zvxf archive.tgz
Note that your archive must not include any binary executable files, meaning any compiled programs or intermediate build objects (.o files, for example). This will cause your email to be rejected by most services.