TWiki> CS450fall11 Web>Homework4 (2011-09-13, Main.amusa2)EditAttach

Homework 4

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.

For the grading script to work right, you have to turn off block buffering (the default when stdout is redirected to a file or pipe). Add the following line to the top of your main() function to turn off buffering altogether setbuf(stdout,NULL);. To switch to line buffering, which should work fine as well, use setlinebuf(stdout).

NOTE: There is no template for this homework. It is up to you to create the necessary directory, Makefile, etc.

Here is an example transcript with 3 connected telnet clients:


./hw4 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.

Grading

Do svn up in your /pub. The grading script is in /pub/grading-scripts/hw4/. To run the grading script:

./hw4_score username port score_file

The grading script runs the server and simulate multiple client telnet connection using a python library named telnetlib. It saves output from server and all the clients to files and look for appropriate output in them. You will need to follow the output format exactly with the example output so that grading script finds the output it is looking for.

Following is the sample output of grading script:

[SUCCESS] name works [1]: 1
[SUCCESS] musa and tomas got message from jakob for say [1]: 1
[SUCCESS] james, tomas, jakob, bala got message from musa for say [1]: 1
[SUCCESS] whisper worked between musa and tomas [1.5]: 1.5
[SUCCESS] correct statistics [1.5]: 1.5
Total score: 6

Following is the transcript used for grading

Server:
Got connection (Client 1)
Client 1 set name to musa
musa said: Hi from musa
Got connection (Client 2)
Client 2 set name to tomas
tomas said: Hi from tomas
closing connection...
musa disconnected. A total of 27 bytes received from musa.
closing connection...
tomas disconnected. A total of 29 bytes received from tomas.
Got connection (Client 1)
Client 1 set name to musa
Got connection (Client 2)
Client 2 set name to tomas
Got connection (Client 3)
Client 3 set name to jakob
musa said: hi jakob
tomas said: hi jakob
jakob said: hi musa, tomas
musa whispered to tomas: this message is between you and me only
tomas whispered to musa: sure
Got connection (Client 4)
Client 4 set name to bala
Got connection (Client 5)
Client 5 set name to james
musa said: Hi James, good to see you!


musa:
Client 2 set name to tomas
Client 3 set name to jakob
tomas> hi jakob
jakob> hi musa, tomas
~tomas> sure
Client 4 set name to bala
Client 5 set name to james


tomas:
Client 3 set name to jakob
musa> hi jakob
jakob> hi musa, tomas
~musa> this message is between you and me only
Client 4 set name to bala
Client 5 set name to james
musa> Hi James, good to see you!



jakob:
musa> hi jakob
tomas> hi jakob
Client 4 set name to bala
Client 5 set name to james
musa> Hi James, good to see you!



bala:
Client 5 set name to james
musa> Hi James, good to see you!


james:
musa> Hi James, good to see you!


Topic revision: r4 - 2011-09-13 - 16:42:22 - Main.amusa2
 
Copyright 2016 The Board of Trustees
of the University of Illinois.webmaster@cs.uic.edu
WISEST
Helping Women Faculty Advance
Funded by NSF