Homework 3

In this homework, we finally get to the mother of all serious socket programming: the select() system call.

select() allows a program to wait for multiple file descriptors at once, whether reading, writing or just looking for error messages from a socket.

Create a simple chat server for telnet clients, using TCP sockets and select(). Let every connected client receive using TCP sockets and every message sent by every other client.

Pay attention to the details. Specifically, you are required to handle the commands "say", "name" and "whisper", keep track of and report (on connection close) the number of received bytes for each client. You should also keep track of and use the name set by the client, as shown below. The number of clients can be more than two, but will be less than 16 during grading.

Here is an example transcript with 3 connected telnet clients:


./hw3 8081
Got connection (Client 1)
Got connection (Client 2)
Client 2 set name to Jakob
Client 1 set name to Musa
Jakob said: Hi Musa
Got connection (Client 3)
Client 3 set name to Tomas
Jakob said: Hi Tomas
Tomas said: Hi everyone
Jakob whispered to Musa: Wasn't he the TA last year? What's he doing here?
Jakob said: Bye bye
Jakob disconnected. A total of 125 bytes received from Jakob.
Musa said: Bye Tomas
Musa disconnected. A total of 26 bytes received from Musa.
Tomas disconnected. A total of 29 bytes received from Tomas.

client session 1:

telnet localhost 8081
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
name Jakob
Client 1 set name to Musa
Hi Musa
say Hi Musa
Client 3 set name to Tomas
say Hi Tomas
Tomas> Hi everyone
whisper Musa Wasn't he the TA last year? What's he doing here?
say Bye bye
^]
telnet> close
Connection closed.

client session 2:

telnet localhost 8081
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Client 2 set name to Jakob
name Musa 
Jakob> Hi Musa
Client 3 set name to Tomas
Jakob> Hi Tomas
Tomas> Hi everyone
~Jakob> Wasn't he the TA last year? What's he doing here?
Jakob> Bye bye
Jakob disconnected.
say Bye Tomas
^]
telnet> close
Connection closed.

client session 3:

telnet localhost 8081
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
name Tomas
Jakob> Hi Tomas
say Hi everyone
Jakob> Bye bye
Jakob disconnected.
Musa> Bye Tomas
Musa disconnected.
^]
telnet> close
Connection closed.

Hints

The selectserver.c example will give you an excellent start, use it. My solution is 207 lines, including lots of whitespace and comments. selectserver.c is 134 lines, so you can figure the solution is about 50-100 lines of code.

In my solution, I use the following string functions:

  • strcmp for comparing strings
  • sprintf for composing formatted strings from various parts
  • sscanf for cutting out parts of strings
  • strcpy for copying strings
  • strlen for figuring out string length (don't forget about the null-terminator after!)

TIP: you'll need to refactor the socketserver code (move stuff around) quite a bit to make it work well.

Topic revision: r1 - 2010-09-13 - 17:16:33 - Main.jakob
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF