Socket Programming | Advanced Computer Networks | ECE 545, Study Guides, Projects, Research of Electrical and Electronics Engineering

Material Type: Project; Class: Advanced Computer Networks; Subject: Electrical and Computer Engr; University: Illinois Institute of Technology; Term: Fall 2005;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 08/19/2009

koofers-user-7w0
koofers-user-7w0 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PROJECT REPORT
SOCKET PROGRAMMING
ECE545, Fall 2005
Submitted by: Yimeng Li
Zeping Shi
Ling Lu
Date: 09/19/2005
pf3
pf4
pf5

Partial preview of the text

Download Socket Programming | Advanced Computer Networks | ECE 545 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

PROJECT REPORT

SOCKET PROGRAMMING

ECE545, Fall 2005

Submitted by: Yimeng Li

Zeping Shi

Ling Lu

Date: 09/19/

I. How the 1-to-1 Chat Room is implemented using the given code

The give code provides a simple example with limited functions. There are two main modifications made to implement the 1-to-1 Chat Room.

 Two-way communication.

To implement two-way communication, both the client and the server create two threads, sender and receiver , when connected through socket. The sender is responsible for reading user’smessagefromthekeyboardandsendingittothepeer.The receiver reads message from the socket and displays it on standard output, i.e. the screen. After creating the SenderThread and ReceiverThread, the main thread goes to idle for their termination. By notification through a sender.isAlive() or receiver.isAlive() method, the main thread makes sure the current communication is terminated and then close the socket.

 Both sides can terminate communication.

In this 1-to-1 Chat Room, both sides are capable of terminating communication. On the clientside,theusertypescommand“exit”(thenitexits),whichwillnotonlycloselocal socketbutalsotriggera“CLOSE”messagetotheserver.Whenreceivinga“CLOSE” message, the server closes its communication socket and goes on waiting for another connection.Ontheserverside,whenusertypes“exit”,theserverclosesits communicationsocketandsendsa“CLOSE”messagetotheclient.Whenreceivingthis message, the client closes its socket and exits.

II. User requirements

There are few requirements on users. For the server side, the default port number will be used if user does not specify explicitly. For the client side, both the server address and port number are parameters when the program is invoked. Also, default values will be used if not explicitly specified by user.

III. Architecture

For 1-to-1 Chat room, messages are directly delivered to remote side, no intermediate node required for message forwarding. It is the simplest duplex network communication model.

Client Server User User

For Many-to-many Chat Room, a server works as a central message processor which receives messages from any one of the on-lineclientsandthen“broadcasts”themtoalltheusers.

a. Check if the packet comes from a new client. If so, add this client in the Client- List. b. Display the message on the server-end. c. Add a sequence number to the packet and send it to all the clients in the Client- List. The sequence number added here will help us to do the message ordering.

  1. Special cases. a. Iftheincomingmessageis“TERMINATE”,whichwillbesentwhenuserpunch the“Exit”buttononclient-end. The server deletes this client from the Client-List and then notifies all the other users. Ifthelastclientquittheconversation,theserver’stypingfieldwillbedisabled. b. Ifthe“StopService”buttonontheserver-end is punched, a message about this action will be send all the existing clients. And the server closes the socket and exits.

Client-end:

  1. Initializing. Once running, the client opens a socket (default 5001) waiting for incoming messages, and spawns a consumer thread which checks the message buffer and displays the message.
  2. Processing packets. If any packet comes, the client will spawn a producer thread which pushes the packet to the message buffer.
  3. Message Buffer detail. This buffer is introduced in due to message ordering. Its method getReadyMsgPkt (int expectingID) is responsible for getting the expecting message packet. It will a. Check the buffer if there is no message at all, it will wait until it is notified to resume. b. Once resumed (notify signal is issued by producer thread), it gets the message withthesmallestIDinthebuffer.Andthencheckifitistheonewe’re expecting. If yes, return the message. If not, it will schedule a timed task for return this message.Iftheexpectingmessagecomesbeforetimeout,we’llreturnthe expecting one. Otherwise, the one with larger ID than expected will be returned. c. Loop on (b) until no message available, then go to step (a)
  4. Displaythemessage.Clientwon’tdisplaythemessagewhenuserpunchtheenterkeyin the typing field. Instead, the message seen by user is what the server sends back. This will help us to do the message ordering. The Client keeps the information of the expecting packet ID. When getting the message, it passes the expecting ID as an argument to the message buffer. And it keeps updating the expecting ID as the conversation goes on.
  5. Special cases. a. The Clientcanquittheconversationbyclickingthe“exit”button

IV. Message ordering

A. TCP implementation

Message ordering in this TCP implementation can be categorized as follows.

Ordering for single client

It is the simplest case in the whole question. As a connection-oriented reliable service, TCP supports message ordering through its byte stream connection. For each client-server pair, messages are delivered and received in the exact order they were sent. The client does not display its own message until it receives echo-back from the server.

Ordering for multi clients on server receiving

On server side, this is a Many-to-one communication model. Message ordering is achieved by multi-threaded programming when messages are received by the server. Every client connection has a dedicated ServerReceiverThread , which is responsible for receiving messages from its dedicated client and forwarding them to all the on-line clients immediately. The concurrent running of all the ServerReceiverThread s makes it possible to receive messages from different clients in the original order they arrive at the server.

Ordering for multi clients on server broadcasting

Fromserver’sviewpoint,thisisaOne-to-many communication model. It is the most complicated case in the whole question. Theoretically, to keep messages orderly on sending to multi clients requires the server sending each message simultaneously on all the communication sockets. That means certain number of threads should be created to perform this task concurrently. But the question is: How to create these threads simultaneously? Fortunately, on the fact that the server runsveryfast(that’struefortoday’smachine),asimplefor(;;)loopcanvisiteachconnectionand send message to it very fast that its performance is very much approximate as the message is sent to different destinations at the same time.

B. UDP implementation

Message ordering in UDP Chat Room is accomplished by the following mechanism.

 Clientwon’tdisplaythemessageitsendsimmediately.It will only display the message when the server sends it back.  On receiving a message, the server appends a sequence number to the message and sends it back to all the active clients.  Client will check for the sequence number before display the message. If the sequence numberistheoneit’sexpecting,itwilldisplayitrightaway.Otherwise,itwillholdthe message while waiting for the expecting message. If the expecting message comes before timeout, it will get displayed. Otherwise, it will be ignored and the latter message will be displayed.  Thus, we achieve the message ordering, all the messages displayed at the client-end will be of the same sequence as they arrive at the server-end. (Kickout button is not functioning)

V. Difference between the TCP and UDP implementations

As shown above, the Chat Room can be implemented in both TCP and UDP. A brief comparison of them is shown in the table below.