You are on page 1of 5

aaaa

Java Chat Server

Introduction
This chat server is written entirely in Java and can support multiple users at the same time. The server can initalized an any unused port the user wishes, on any machine which can support Java. If this computer is connected to a network, the clients can be started on any machine which can access the server, including the machine the server is running on. The chat client is also written entirely in java and can be started on any machine which supports java. From the user interface, the client must specify the machine and port to connect to where an instance of the chat server is running. After the server has been started, multiple users can connect to and exchange messages. The messages can be either broadcast to the entire group of currently active users, or it can be sent privately to one particular active user. The clients are free to join and leave the chat as they wish, but no two clients are allowed to be logged on with the same username at the same time.

Theory of Operation
The stand-alone Java chat server proxy called "chatserver.java", will correctly do the following:

Read a local port to set the server running on as a command line argument. Create a socket and accept chat packets on the local port. Create a thread to handle each packet which arives at the server: o Parse the packet and decide what to do with it o Send the resulting packet to one client or broadcast to all clients Close the sockets if an exception occurs. Correctly terminate the threads. After the sockets are closed, connect more clients without re-starting the chat server. Complete each of these tasks for multiple concurrent users.

Each of these tasks is implemented entirely in Java, and can be run on any machine which supports Java.

Implementation

Read the local port as command line arguments: This is done by by parsing the commands entered by the user when the chat server is initailly started, picking out the port to operate on. This is checked for legality, and passed to the rest of the program if acceptable. If invalid, the program would exit and report the appropriate error.

Create a socket and accept chat packets on the local port: This is accomplished by creating a new Datagram Socket. The port which it listens on is the one passed by the user on the command line.

Create a thread to handle each packet which arives at the server: This is done by implementing a new Class, chatserverThread which extends the Thread Class, and creating new chatserverThread, which accept a Socket and a Hashtable as its parameters. The Socket that is passed is the DatagramSocket created when the server is started. The Hashtable is the main Hashtable which keeps track of the current users logged onto the server.The Run() method of the Thread Class is overwritten, and does the actual data stream transferring The chat server accepts data on the datagram socket with the use of the recieve() method, and stores it in a buffer.

Parse the packet and decide what to do with it: There are many methods which look at the packet and test it to se what type of packet it is. If it is a join packet the username is inspected to see if it a valid one. If it is, the user is entered into the HashTable as an active client. Otherwise the user is not entered. If it is a leave packet, the username is checked against the HashTable for validity. If the packet is a message packet, the sender and reciever IDs are checked against the HashTable for valididty.

Send the resulting packet to one client or broadcast to all clients: The server will make each packet with the MakePacket() method. It will then send the packet to one client or all clients, depending on how certain flags were set when the packet is parsed.

Close the sockets if either closes or an exception occurs: If any type of error is detected while the server is either started or running, it will immediately close all sockets, and terminate itself, unless it is an error that is not fatal to server, in which case it will remain running, serving more clients.

Correctly terminate the threads: Again, if any type of error is detected while the server is either started or running, it will immediately end all threads, and terminate itself, unless it is an error that is not fatal to server, in which case it will remain running, serving more clients.

After the sockets are closed, connect more clients without re-starting the proxy: Once the server is started it will continualy listen for new connections on the port it is listening to. Therefore, if one connection is ended, thereby closing the sockets and terminating the threads, the chat server will still be active, listening for more users to connect. It does not need to be restarted in order to be connected to by more clients, because it runs independently, with or without users currently using it.

Complete each of these tasks for multiple concurrent users: Because the server creates a new thread for each client which attempts to connect to it, it can support multiple concurrent users simultaneously without any problem

Read the local port as command line arguments: This is done by by parsing the commands entered by the user when the chat server is initailly started, picking out the port to operate on. This is checked for legality, and passed to the rest of the program if acceptable. If invalid, the program would exit and report the appropriate error.

Open a User Interface: This is done by initailizing a new userinterfaceframe object when the client is started. This object containd the actual user interface that the user will see.

Parse the user input from the user interface This is done via various parsing methods which take the feilds from the user interface and pick them apart.

Send the packets to the server This is done by sending the created packet out on the DatagramSocket created when the user interface is created.

Create a socket and accept chat packets at the client:

This is accomplished by creating a new Datagram Socket to listen on when the client is intialized.

Create a thread to handle each packet which arives at the client: This is done by implementing a new Class, clientrecieve which extends the Thread Class, and creating new clientrecieve, which accept a Socket and a reference to the User Interface, and a reference the the text area of the User Interface as its parameters. The Socket that is passed is the DatagramSocket created when the client is started. The User Interface is the one the client is working with. The text area is the one that the messages will get presented to the user on. The Run() method of the Thread Class is overwritten, and does the parsing of the incoming message.

Parse the packet and decide what to do with it: If the user has just started the client, the only feilds of the User Interface they will have access to are the Machine:Port input and the Username input. When they send a join packet to the server, they will only recieve a join confirmation packet is the server can accept their username. In this case the packet coming back to the client will be a join packet as well. The clientrecieve thread checks for this, and updates the status of the User Interface only if this is the case. Otherwise the user is forced to enter another username and try again.

Output the message contained in the packet to the user interface: The clientrecieve thread will ouput the message to the text area of the user interface by appending the message to its reference to this

Close the sockets if either closes or an exception occurs: If any type of error is detected while the client is either started or running, it will immediately close all sockets, and terminate itself, unless it is an error that is not fatal to client, in which case it will remain running, sending and recieving more packets.

Correctly terminate the threads: After the user has decided to leave the chat, the sockets are closed and the threads are ended

Source

The Chatserver Class

The ChatServer Class implements the Main () method of the chat server. It accepts and parses the users input, while doing some basic error checking. It then creates a new chatserverthread to handle each incoming packet a new hashtable to store the client entries.

The ChatServerThread Class The ChatServerThread Class implements the actual threads which the chatserver Server uses to send and recieve data.. It starts the actual movement of data on the thread with the use the Run() method of the Thread Class.

The ClientInterface Class The ClientInterface Class implements the Main () method of theclient. It creates a new userinterfaceframe which is the actual user interface the client will see. It also creates the new clientrecive thread to handle incoming packets.

The ClientInterfaceFrame Class This Class creates the actual user interface that the user will interact with.

The ClientRecieve Class This Class handles incoming packets from the server. It listens on the Datagram Socket that is passed to it. When a packet arrives is parses it, and takes the appropriate action. In all cases it appends the message to the text area of the user interface.

The ClientsTableEntry Class This Class is implemented to create objects to store in the hashtable to represent each active user. These objects contain the username, IP address, and port of each user

You might also like