You are on page 1of 7

CG3204L PROGRAMMING ASSIGNMENT: CHAT APPLICATION SEMESTER 2 / AY 2013-2014 SUBMISSION DATE: BEFORE 5 PM APRIL 4TH 2014

1. This is an individual assignment. 2. The programming language to be used is C/C++ 3. Please upload a tarball/zip of your source code and Readme file with the name CS3103_PA_<Matric_No>.zip into the IVLE Workbin Student Submission folder before the submission date. Please specify your matric numbers clearly in the Readme file. 4. There is a 10% penalty per day for late submission 5. Demonstrations will be done on April 7th and 8th, using the code uploaded on IVLE. 6. For any clarification on the assignment, please email Ms Sathya at a0110313@nus.edu.sg, or post your questions on the IVLE forum.

OVERVIEW
In this assignment, you will be implementing a chat server and client application using the C/C++ programming language, the pthread library and the Socket library on a UNIX or Linux system. This application allows users to chat with one another in near real-time, and set up group chats.

REQUIREMENTS
The following functionality should be implemented correctly. Additional features are welcome, and can fetch bonus points. Your application should consist of two components The server side and the client side. The server program must prompt for the service port to be used. Clients connect to the server on this port. The server must provide the following functionality 1. Support concurrent connections from multiple clients (using the pthread library) 2. Display the Clients IP:Port for each connection, as well as the Servers IP:Port 3. Support individual/group chat between clients The client program must prompt for the servers IP and Port number to connect to, and a user name to use. The user name is used to uniquely identify the users. Once it has connected to the server, it must display the Servers IP:Port and the Clients IP:Port. The client program should support the following commands 1. help : Display a list of commands supported 2. show : Display the list of users who are connected to the server 3. talk <user_name> <msg> : Send a message msg to the user user_name 4. yell <msg> : Send a message msg to all users currently connected to the server 5. creategroup <user_name1> <user_name2> <user_name3> ... : Create a group chat with the users user_name1, user_name2, user_name3, ... Each of the users must be prompted if he/she wants to join the group chat. See the sample execution for more details. 6. discuss <msg> : Send a message msg to the users in the group chat just created

7. leavegroup : Leave the current group chat 8. exit : Close the connection to the server and exit the client program. The server and other clients should display a notification that the user has exited A user must be able to privately chat with other users using talk command even when a group chat is going on. For simplicity, you can assume that a user does not login to the server from more than one location, and that a user is involved in at most one group chat at a time (i.e. a user cannot join more than one group chat at a given time).

SAMPLE EXECUTION
This section gives an example of how the server and client programs may execute. You need not follow the same format, but the functionality should be the same. In this example, the server and clients are all running on the same machine. Four users Bob, Alice, Ted and Barney connect to the server using the client program. Bob, seeing that Ted and Barney are online, sets up a group chat with them. Both Ted and Barney are notified of the invitation to the group chat. Ted declines, while Barney accepts. Bob and Barney chat in this (rather small) group chat. After discussing, Bob and Barney leave the group chat. Alice, feeling bored, later disconnects from the server using exit. Comments are shown in a slanted font, and are not part of the actual output. User input is in bold.
Server $ ./server === Welcome to the Chat server!! === Server prompts for the Service port to use Enter service port : 4321 Chat Server Running on 127.0.0.1:4321 Bob connects to the server Client bob connected from 127.0.0.1:58101 Another user Alice connects to the server Client alice connected from 127.0.0.1:58202 Another user Ted connects to the server Client ted connected from 127.0.0.1:58303 Another user Barney connects to the server Client barney connected from 127.0.0.1:58404 Bob creates a group chat with Ted and Barney Client bob requests group chat of {bob, ted, barney} Ted rejects the group chat request Client ted rejects group chat request Barney accepts the group chat request Client barney accepts group chat request Bob leaves the group chat after chatting with Barney Client bob leaves group chat Barney leaves the group chat too Client barney leaves group chat Alice uses the exit command Client alice exited from 127.0.0.1:58202

Bobs client $ ./client === Welcome to the Chat client!! === Client prompts for the Server to use Enter Chat server IP : 127.0.0.1 Enter Chat server port : 4321 Client prompts for the user name to use Enter user name : bob Bob connects to the server Client bob 127.0.0.1:58101 connected to Server running on 127.0.0.1:4321 === Welcome bob to CS3103 Chat! === 1. show : Show all users online 2. talk <user> <message> : Send message to user 3. yell <message> : Send message to all users 4. creategroup <user1> <user2> ... : Create group chat 5. discuss <message> : Send message to users in the group chat 6. leavegroup : Leave group chat 7. help : Display all commands 8. exit : Disconnect from Chat server Bob checks who are online > show === Users online === 1. bob (you) 2. Alice 3. Ted 4. Barney Bob creates a group chat with Ted and Barney > creategroup ted barney Group chat request sent

>
Ted rejects while Barney accepts ted rejects group chat request > barney accepts group chat request Bob discusses with Barney > discuss Hey Barney! What plans tonight? > Barney says: Were having a party on the roof. Its going to be LEGENDARY!! Bob can still talk privately with any user even though group chat going on > talk alice Hey Alice, shall we meet this weekend? > alice says: Sure, Saturday Starbucks at 5 Bob leaves the group chat he was having with Barney > leavegroup You have left the group chat > Bob gets a notification that Alice has exited alice has logged out > Teds client $ ./client === Welcome to the Chat client!! === Client prompts for the Server to use Enter Chat server IP : 127.0.0.1 Enter Chat server port : 4321

Client prompts for the user name to use Enter user name : ted Ted connects to the server Client ted 127.0.0.1:58303 connected to Server running on 127.0.0.1:4321 === Welcome ted to CS3103 Chat! === 1. show : Show all users online 2. talk <user> <message> : Send message to user 3. yell <message> : Send message to all users 4. creategroup <user1> <user2> ... : Create group chat 5. discuss <message> : Send message to users in the group chat 6. leavegroup : Leave group chat 7. help : Display all commands 8. exit : Disconnect from Chat server Ted gets an invitation to a group chat, but refuses > You received an invitation from bob to group chat with {bob, barney} Accept? (y/n): n Invitation rejected > Note that if Ted had joined the group chat, he would have got a notification on screen that Barney accepted the invitation Ted gets a notification that Alice has exited alice has logged out > Barneys client $ ./client === Welcome to the Chat client!! === Client prompts for the Server to use Enter Chat server IP : 127.0.0.1 Enter Chat server port : 4321 Client prompts for the user name to use Enter user name : barney Barney connects to the server Client barney 127.0.0.1:58404 connected to Server running 127.0.0.1:4321 === Welcome barney to CS3103 Chat! === 1. show : Show all users online 2. talk <user> <message> : Send message to user 3. yell <message> : Send message to all users 4. creategroup <user1> <user2> ... : Create group chat 5. discuss <message> : Send message to users in the group chat 6. leavegroup : Leave group chat 7. help : Display all commands 8. exit : Disconnect from Chat server Barney gets an invitation to a group chat, and accepts > You received an invitation from bob to group chat with {bob, ted} Accept? (y/n): y Invitation accepted > Ted rejects the group chat ted rejects group chat request > Barney discusses with Bob bob says: Hey Barney! What plans tonight?

on

> discuss Were having a party on the roof. Its going to be LEGENDARY!! Bob leaves the group chat > bob has left the group chat Barney leaves the group chat too > leavegroup You have left the group chat > Barney gets a notification that Alice has exited alice has logged out > Alices client $ ./client === Welcome to the Chat client!! === Client prompts for the Server to use Enter Chat server IP : 127.0.0.1 Enter Chat server port : 4321 Client prompts for the user name to use Enter user name : alice Alice connects to the server Client alice 127.0.0.1:58202 connected to Server running on 127.0.0.1:4321 === Welcome alice to CS3103 Chat! === 1. show : Show all users online 2. talk <user> <message> : Send message to user 3. yell <message> : Send message to all users 4. creategroup <user1> <user2> ... : Create group chat 5. discuss <message> : Send message to users in the group chat 6. leavegroup : Leave group chat 7. help : Display all commands 8. exit : Disconnect from Chat server Alice gets a chat message from Bob and replies to it > bob says: Hey Alice, shall we meet this weekend? > talk bob Sure, Saturday Starbucks at 5 Alice exits > exit You have logged out $

Note that a user can group chat and private chat at the same time (as shown in the sample). You also need to provide notifications to a user about 1. Other users logging out (but you are not required to notify about new users logging in) 2. If the user does not decline an invitation to a group chat, he/she also gets notifications about other users in the group who accept/reject the group chat invitation, or leave the group chat There are many exceptions that can happen which are not shown in the sample execution for example, the server process is killed, client processes are killed without using exit command, a user tries to accept more than one group chat, user names are not unique, etc. It is understandably difficult to take care of all these exceptions in a programming assignment. You can perhaps implement one or two small checks (such as checking if user name is unique). During the demo, please describe the exception cases you have handled. Program robustness

does not carry much marks (see Evaluation), so focus on implementing the functionality described. Please note though that in actual industry standard code, all exceptions must be handled. You are also not required to timestamp the chat messages, and the chat messages can possibly appear out of order (For example, if a group chat contains {Bob, Alice, Ted and Barney}, and Bob & Alice send messages almost simultaneously to the group, it is possible that Teds client displays Bobs message first while Barneys client displays Alices message first). However, the chat messages MUST be in near real-time (i.e. the delay should not exceed more than a few seconds for a small set of users), otherwise it is not possible to have an interactive group chat. You are welcome to add extra features (such as adding/removing users from a group chat after it has been created, logging chat history, etc). Additional features will be given bonus marks.

GUIDELINES
1. You can find information on UNIX Socket Programming in [1, 2]. A tutorial on Pthreads is given in [3]. The chapters on Threads (Chapter 11), Sockets (Chapter 16), and I/O Multiplexing (Section 14.5) in [1] should be sufficient to do this programming assignment. If you have difficulty understanding these topics, please contact me by email so that I can help you. 2. To perform multiplexed I/O on the socket and terminal input on the client side, you can use the select() system call. On the server side, you can use threads to handle many users concurrently. 3. Remember to convert between host order and network order while sending/receiving data, using the hton() and ntoh() functions. 4. Try to think of the tradeoffs while doing your design. For example, after establishing a group chat, would it be better for the clients to communicate with each other directly, rather than through the server? Would it better to use TCP or UDP? Is it better to have two channels (one for control and one for data) or just one channel? Is it better to store the state information at the server, or at the clients? Are there any NAT/Firewall issues? Does your solution scale with number of online users? There is no one best answer for these questions. During the demo, I may ask you about the tradeoffs in the design you implement. If your implementation is not fully working, but you have understood the design tradeoffs well, then you will still get good marks. 5. You may find it useful to represent the working of the client and server using state diagrams. State diagrams make it easier to visualise and validate your protocol.

EVALUATION AND DEMO


During the demo, please bring a printout of your source code, with your matric numbers and group number written clearly on top. You are encouraged to bring your laptops for the demo. If you are unable to bring your laptop, then make sure that your code can compile and run correctly on Ubuntu 10.04 (Linux). Please note that during the demo you are expected to download the source code uploaded in IVLE, compile it and execute it. The tarball in IVLE should contain the source code and a README file. The README file must have your matric numbers, IVLE group number, instructions on how to compile and run the

code, the platform (OS) you developed on, some sample output, and a brief description of how the code works. This assignment counts 15% to your final grade. The total points is 100. Source Code compiles properly: 5 Client connects to server: 10 Multiple clients can connect to server: 10 Client correctly displays users online: 10 Talk & yell commands work properly: 15 Group created correctly and invitations sent properly: 15 Group chat messages sent and received properly: 15 Client Notifications displayed correctly: 10 Code neatness and documentation: 5 Robustness: 5 (Total 100 points) Bonus: 10 Bonus points are given for any extra features/functionality you implement. Please note that during the demo, if you have worked in pairs, I will ask what work each of you has contributed to the assignment.

REFERENCES
[1] W Richard Stevens and Stephen A Rago, Advanced Programming in the UNIX Environment, Addison Wesley, 2nd Edition [2] W Richard Stevens, Bill Fenner and Andrew M Rudoff, UNIX Network Programming Volume 1: The Sockets Networking API, Addison Wesley, 3rd edition [3] Blaise Barney, POSIX Threads Programming, 2009 https://computing.llnl.gov/tutorials/pthreads/ [4] Linux Online Documentation http://linux.die.net/

You might also like