The first file has the client side of the application, the second contains everything related to the server and the last contains both... (Don't waste even a few KBs of your download !!!). Though you can interface your Server or Client sides with mine by changing protocol headers, I recommend using them for the best results...
Introduction
Yet another Server/Client model with a good looking swing front-end... Though there are lots of Server/Client models lying around, I've tried to design the easiest... You're free to use this source code for anything you wish, recommended that you at least understand it properly!!!
Background
Well, there's no particular background for this project... We had a mini-project to do using JAVA. We got Server/Client as our topic... So I surfed the internet, went through a lot of other articles, code, etc. regarding similar topics and concluded that the code could have been easier & UI could have been finer... If you are smart at reading code and go directly for code, comments are inserted... Total newbies can get some direction from this article.
Server Side
Server-side of the application comprises three files:
- InfiniteChatServer.java
- InfiniteChatClinetHandler.java
- InfiniteChatClientObject.java
InfiniteChatServer.java
Here, the file 'InfiniteChatServer.java' is the highest in hierarchy. It creates a GUI to accept the port number to set up a server socket at given port. This is managed using a JTextField
. GUI provides three more JButtons
(!), to start, stop and exit out of iChat Server respectively. When the server is running, the 'start' button is disabled and when server is not running, 'stop' button is disabled. 'Exit' button is enabled all the time giving the user the option to exit whenever he feels like.
Whenever user hits the start button, the method start_iChatServer()
is invoked. This method retrieves the port number given by the user and sets up the server at that port. If any error occurs, the server is set up at default port, that is port number 1664
. Some changes are made to the GUI to ensure safety of the application. The server is set up using an instance of ServerSocket
at given port number. It then creates a thread named 'iChat Server v1.0' and start()
method for this thread is called. start()
method in turn calls the run()
method overridden in the class. In run()
method, this thread waits and accepts incoming connection from the clients. When a connection is accepted, an instance of 'InfiniteChatClientHandler
' is initialized. This file further has many important functions such as to check whether user with a given username already exists, add a user, remove a user, send message to single user (unicast), send message to all the users (broadcast), etc. Connected users' information is stored using an ArrayList
, which contains objects of type 'InfiniteChatClientObject
'.
Hence, we can say that the class 'InfiniteChatServer
' is the highest in hierarchy.
InfiniteChatClientHandler.java
This class creates a thread for every client connected to the server. An instance of this class is initialized with every accepted incoming connection. Now all the traffic from this client is processed by this instance.
The constructor for this class is declared as:
public InfiniteChatClientHandler(InfiniteChatServer server,Socket client);
Here, the 'InfiniteChatServer server
' gives the reference of the instance of server from which the constructor was invoked and 'Socket client
' gives the reference of the client to be handled by this instance of the class. The class uses a BufferedReader
to read from the client socket. Further, the constructor creates a thread of itself and invokes start()
method. The start()
method invokes run()
method overridden by the class. In run()
method, all the traffic from the client is monitored and processed until the thread stops.
InfiniteChatClientObject.java
This is a simple class used to encapsulate client details. It wraps a reference of the client's Socket
, and a String
to store username. Two constructors provided include a do-nothing constructor and another constructor, which is declared as:
public InfiniteChatClientObject(Socket clientSocket,String clientName);
Here, Socket clientSocket
provides the reference to the client's Socket
and String clientName
gives client's username. Methods are provided to store and retrieve these client details.
Client Side
Client-side is the counterpart of server-side of the application. I won't be giving brief explanations about the code, as the code is self-explanatory, contains comments and is really straight forward. Client-side consists of the following files:
- InfiniteChatClient.java
- InfiniteChatClientLogin.java
- InfiniteChatClientHandle.java
- InfiniteChatClientUI.java
- InfiniteChatClientListRenderer.java
- InfiniteChatRoomUI.java
- ImagePanel.java
- DialogBox.java
InfiniteChatClient.java
The iChat client also uses swing light-weight components to create an easy to use GUI. In iChat client, the file 'InfiniteChatClient.java' can be said to be the highest in the hierarchy.
All the traffic from the server is handled by this file. It stores username, servername, port number, etc. and keeps track of ongoing conversations. This file provides methods to send message to server, start a new conversation, log out of the application and so on... The main()
method of this file instantiates an object of type 'InfiniteChatClientLogin
'.
InfiniteChatClientLogin.java
When the application starts, a login form is displayed by this file. It uses JTextFields
to accept username, servername and port number. A username can be anything else than a blank space. The only restriction apart from this is that the username shouldn't already be in use. Servername
is the name or IP address of the computer on which server part of the application is running and port number is the port of the server to which client should connect (It's the same number you typed for the server side of the application...). Two JButtons
provide options for logging in or to exit.
When a user logs in, an instance of 'InfiniteChatClientHandle
' is created and it is passed to the 'InfiniteChatClient
' instance, from which it was invoked.
InfiniteChatClientHandle.java
This class just stores details about the user and defines methods to store and retrieve these details. Thus, it acts as a handle to the current user.
InfiniteChatClientUI.java
When a user logs in, he can see the list of users online and buttons to log out and exit. All this GUI is created by this file. If a user wants to start chatting with another user, he just needs to double click on that user's name. This will open a chat room.
InfiniteChatClientListRenderer.java
This class is used to render the online users list. The icon before each username is displayed using this file.
InfiniteChatRoomUI.java
This file is used to display UI of a chat room. Chat room has a button named 'save', which can be used to save your conversation with another user. The chat room remains as it is till you close it manually, even after logging out or exiting the application. This is done to ensure that you can save your conversation whenever you want.
ImagePanel.java
This class is used to display the logo of the iChat application on chat rooms.
DialogBox.java
It is used to display warning and error messages, i.e. warnings about missing fields, errors such as unavailable username, etc.
Conclusion
So as to conclude, you now know how the code provided for download works. You have a (?@#!!! well... actually another) working server-client model. You can use it for anything you like and keep looking for my upcoming projects.
So...
Stay tuned... Take care...