IT-højskolen /Kurser E2003 /Introduktion til multimediesystemer
Networking in Java

Theoretical Exercise

There is no theoretical exercise this week


Practical Exercise


This weeks exercises will focus on network programming using Java. In the Java networking handouts there were a couple of examples. These can be downloaded here:

EchoServer.java, InetAddressTest.java, MailTest.java, SocketTest.javaThreadedEchoServer.java


All of these examples will to some extend be important for the assignment (with a possible exception of InetAddressTest.java).

Chatroom

In this weeks exercise we will make a simple chat room. The purpose of the chat room is to enable several people to connect to the chat room-server and  write messages to all the other people also connected to the server. 

Server Components

In order to limit your workload the outline of the Client and the Server have been made. In the figure below an illustration of the server components are shown.

 

 

The idea is that the main program waits for new clients to connect. When a client wants to log on to the server, the main program spawns a new thread which is dedicated to handle messages from that client.  Initially this thread sends a welcome message and receives the username. The username can be used for unique identification of a user. That is, the server may reject a client from logging on if another user has the same username.  However, this is not mandatory for  this assignment.  Either the main program or the thread should insert information about the client in the clients list, in order to updated information about which clients are connected to the server. This is obviously necessary  if we want to send a message to all the other clients connected to the server.

In order to send messages from one user to all the others we need to have two pieces of information, namely the message to be sent and the set of users to send the message. This information is stored in two different data structures.  The first data-structure, "Message Buffer" contains the messages to be broadcasted. This data-structure may be implemented as a buffer. The buffer is a nice data structure for this purpose since the message received first will also be the first to be "broadcasted". The second data-structure contains the set of clients connected to the server. A set may be implemented as a list of items.   The clients list may contain the addresses of all the clients or merely the list of java PrintWriter objects. Both data structures should used mutual exclusion or semaphores since several threads may access these concurrently. 

I have provided the implementation of Buffer.java and sList.java for the buffer and set implementations. You may use these or make your own implementations, as you wish. One instance of each can be found in the State class in Server.java. The state class should contain the necessary information about the current state of the system i.e. the messages recieved and the users!!!

The final component used in the server is the broadcast thread. The thread should take one message from the "Message buffer" send it to each user in the "Clients List " .

Client Components

There are only two components in the the client -  namely the GUI and a Receiver thread. The thread is used, as in the Server, for recieveing messages from the server and print these in the GUI.  

What to do

You have to implement the client and server such that system mentioned above is functioning.  In the code the places shown by "//TODO" indicates places where code is definitely missing

The report is due in 3 weeks from today and should include your source code, a description of what you have done and a the path to where I can find the implementation so that I can test it. 

Limitations:

There are several  limitations to the system mentioned above. 

The most prominent is that you should not expect the system to have a consistent time-ordering. That is the messages may not appear in the same order on all the clients. In order to prevent this much more sophisticated algorithms are needed. 

You may further more experience that if the message sent contains returns it will be split up into several smaller messages and may therefore appear as if they were independent messages and may further more be interleaved by other messages. 

Finally several users may have the same username, hence it may be difficult to see who really sent what messages.

You may possibly find more restrictions 



Updated 2003-11-03

til top