This page does not represent the most current semester of this course; it is present merely as an archive.
In this assignment, you’ll take the server code we supplied and the client code you wrote in lab 9 and combine them to make a command-line 2-party chat program.
Write a single file called schat.c
. Use only C library functions and code you wrote in it: no third-party libraries or C++.
schat.c
should have a main
function that accepts command-line arguments. If given no arguments, it should run in server mode; if given two arguments (the first an IP address, the second a port number) it should run in client mode, connecting to that server.
The server should begin by
accept
only once, unlike the lab server’s accept
loopclose
the listen
er socket before working with the accept
ed socketThe client should begin by
connect
ing to the given IP address and port, like your lab client didBoth should then proceed as follows:
poll
1 to pick either the connected socket (from the server’s accept
or the client’s connect
) or the standard input stream2 to read
3 from. Use a 1-minute4 timeout for poll
.poll
returns a positive number (i.e., it succeeded),
revents
including POLLIN
, read
from standard input and write
what you read to the socket.revents
including POLLIN
, read
from the socket and write
what you read to standard output5. You may assume no single message is more than 4096 bytes, to avoid needing to loop your read
/write
calls.This assignment does not require you to handle the following in any particular way:
poll
call times outYou do need to avoid buffer overflows, use-after-free, and other memory bugs. You are also invited to add sane behaviors for all of the above cases, but are not required to do so.
In a simple implementation, once the client and server connect everything that is typed in one will appear in the other after you press enter.
Server | Client |
---|---|
$ |
$ |