You are on page 1of 4

Lab 2: Socket Programming

ECE 356 Computer Network Architecture Duke University Fall 2012

1 Lab Overview
1.1 Introduction
Peer-to-peer applications are a common feature of computing today. Services from BitTorrent to Skype all rely on direct client-to-client contact. In contrast, Client-Server applications have strict divisions in the functional roles each component takes, oftentimes for purposes of efficiency in resource sharing. In this programming assignment, you will be creating two versions of a simple chat application based on two of the three application architectures discussed in lecture: (1) Client-Server, and (2) Peer-to-Peer & ClientServer Hybrid. The purpose of this assignment is to get you familiarized with the architectures, understand their pros and cons, and be able to identify what type of application each architecture is appropriate for.

1.2 Network Programming


The goal of this assignment is to also learn socket programming in a client-to-server and peer-to-peer context. You can use any IP addresses you would like for communication. The use of the C programming language is strongly encouraged, but not required. Other languages that are acceptable include Java and C++. If you are using any language other than the ones listed above, you must email the TAs for approval before you begin the assignment. This is to ensure that you are truly using the sockets to interface with the networkdont use any libraries that abstract away the networking part of this network application. Note that the TA tutorial sessions on this programming assignment will be done in Java. If you are using C, you have to use system calls like socket(), bind(), listen(), accept(), connect(), and close(). For a detailed tutorial refer to Beejs guide: http://beej.us/guide/bgnet/output.print/bgnet_USLetter.pdf Dont hesitate to seek out other tutorials or reference sources online.

2 Requirements
2.1 Client-Server (45%)
For this application, you need to write a client and a server with the following requirements: The client and server must utilize TCP socket connections. The client must be able to connect to the server and obtain a list of available clients. After obtaining this list, the client must be able to connect to one other client on the list. You may want to consider displaying this list and allowing the client to choose who he/she wants to connect to.

The clients must relay text messages only through the server. That is, each client must send its message to the server (along with some identification regarding which client the message is intended for) and the server will pass along the message to the intended recipient client. You can define whatever convention you would like to indicate that a text message is complete (e.g. hitting [Enter], clicking send). Each client must send an acknowledgement to the server that a text message has been received and displayed. Once the server receives the acknowledgement, it must then send another acknowledgement to the original sender client that the text message has been received and displayed. You must also provide some way to disconnect and end the conversation. After disconnecting, the user should be able to retrieve a new copy of the servers client list and connect to a new client. The connection between the two clients must be duplex, so both clients are capable of transmitting and receiving at the same time. A GUI is not required as this is not the focus of the assignment. You can simply use the command line for input.

NOTE: It is suggested that you first get the Client-Server communication working before implementing the full Client-Server-Client model.

2.2 Peer-to-Peer & Client-Server Hybrid (45%)


For this application, you need to write a client and a server with the following requirements: The client and server must utilize TCP socket connections. The client must be able to connect to the server and obtain a list of available clients. After obtaining this list, the client must be able to connect to one other client on the list. You may want to consider displaying this list and allowing the client to choose who he/she wants to connect to. The clients must then be capable of relaying text messages directly to one another without further help from the server. You can define whatever convention you would like to indicate that a text message is complete (e.g. hitting [Enter], clicking send). Each client must send an acknowledgement to the other client that a text message has been received and displayed. You must also provide some way to disconnect and end the conversation. After disconnecting, the user should be able to retrieve a new copy of the servers client list and connect to a new client. The connection between the two clients must be duplex, so both clients are capable of transmitting and receiving at the same time. A GUI is not required as this is not the focus of the assignment. You can simply use the command line for input.

2.3 Documentation (10%)


You must also provide four additional pieces of written explanation compiled together in a single README (note that this can be a Microsoft Word document or PDF): (1) Include step-by-step instructions on how to run both of your programs. If your programs do not run or you do not provide sufficient instructions, this may negatively impact your grade. Be sure to refer to your program files explicitly by their filename (i.e. Run the p2p_client.java file rather than Run

the peer-to-peer client). Consider having a classmate walk through your instructions on his/her own computer to ensure your programs work on an external machine and your instructions are clear. Label this section INSTRUCTIONS. (2) List any current/known limitations with your chat programs. If you do not have any limitations, state this as well. Label this section LIMITATIONS. (3) Write up a pros and cons explanation regarding the various architectures used in this assignment. For example, why might a hybrid architecture be better for a chat program than a client-server-client architecture? 2-3 paragraphs should be sufficient. Label this section ANALYSIS. (4) Include a brief written documentation of your work to allow a third party, without knowledge of your source code, to write another client that can use your server and talk to your client. Diagrams and flowcharts may be used to minimize the amount of writing you need. 1-2 pages should be sufficient in most cases. MSPaint diagrams and/or hand drawings are acceptable if legible and adequately explained. Label this section DOCUMENTATION.

3 Additional Information


3.1 Hints
Here are a few hints that may help you as you write the program. You have to choose a server port to connect to. Ports from 1-1023 are mostly used for certain services and require administrative privileges. Use port numbers greater than at least 1023. Close your sockets cleanly before exiting the program. If you abort the program, the port may not be freed. You can run all of the processes on the same machine. For your machine, just use localhost. You can use ifconfig (unix) or ipconfig (windows) to determine the IP address for testing across multiple machines Be wary of overzealous firewalls stopping your connections - try temporarily disabling firewalls if you find your connections timeout or are denied. Dont forget that Wireshark can watch what your program is transmitting, possibly helping you during debugging.

3.2 Grading
Partial credit will exist, and significant credit is available to those who demonstrate a functioning program that fulfills the requirements listed above. Clean code, good commenting, and intelligent/descriptive error handling combined are important components of making this assignment manageableand important components of your grade.

3.3 Bonus Points 


Bonus points will be awarded for extra work that improves functionality, and more work will provide significantly more bonus points. For example, the ability to add an arbitrary number of people to the chatroom would improve functionality. Other examples would include the ability to chat with a user behind a NAT, to have distributed list servers, or any other creative extension of the communications component of the application. Bonus will also be awarded if you give the option to use either UDP or TCP. Explain why the difference choices are meaningful in your README. Calculate the round trip time (RTT) of your message under both protocols. Make sure that the RTT includes the time for sending your message and receiving the

acknowledgement. How does the RTT vary with message size? Is there any difference between UDP and TCP in mean RTT? The variance of RTT? Keep in mind that a non-functional application with lots of features is a lot less useful than a simple but functional application. Additionally, GUI improvements will not be counted towards bonus points as this is not the focus of the assignment.

4 Assignment Submission


This programming assignment is to be turned in via email to the TAs (please copy all TAs). Do NOT include the professor on this submission. Attach all source code and documentation listed in the Requirements section. Your assignment submission will not be accepted without a README.

You might also like