Click here to Skip to main content
13,299,898 members (59,803 online)
Click here to Skip to main content
Add your own
alternative version


52 bookmarked
Posted 18 Oct 2008

iChat... Yet Another Server/Client Model...

, 3 Apr 2009
Rate this:
Please Sign up or sign in to vote.
Did it for the sake of a mini-project... Watch out for my real project...

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...


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!!!


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, codes, 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:


Here the file '' 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 done 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.

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.

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: 


The iChat client also uses swing light-weight components to create an easy to use GUI. In iChat client, the file '' 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'.

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.

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.

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.

This class is used to render the online users list. The icon before each username is displayed using this file.

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.

This class is used to display the logo of the iChat application on chat rooms.

It is used to display warning and error messages, i.e. warnings about missing fields, errors such as unavailable username, etc.


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.


Stay tuned... Take care...


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Akshay Bapat
India India
No Biography provided

You may also be interested in...


Comments and Discussions

Bugbroadcasting that the user has logged out Pin
jumanjiarumugam5-Sep-12 4:42
memberjumanjiarumugam5-Sep-12 4:42 
Questionhai Pin
Member 78463589-Feb-12 4:54
memberMember 78463589-Feb-12 4:54 
QuestionLOGGED out message not broadcasted Pin
Member 837880129-Nov-11 6:10
memberMember 837880129-Nov-11 6:10 
Questionhow to broadcast a message Pin
Member 837880129-Nov-11 5:35
memberMember 837880129-Nov-11 5:35 
Questionmsg to all users Pin
Member 837880128-Nov-11 14:02
memberMember 837880128-Nov-11 14:02 
QuestionModify Chat Pin
mustafatekin14-Nov-11 21:17
membermustafatekin14-Nov-11 21:17 
Questionproject report Pin
Member 832536714-Nov-11 3:41
memberMember 832536714-Nov-11 3:41 
Questionplz.. tell me the concepts behind the project for my viva on this project .. Pin
harshaggarwal21-Dec-10 15:38
memberharshaggarwal21-Dec-10 15:38 
GeneralMy vote of 3 Pin
xujianguo26-Jul-10 23:05
memberxujianguo26-Jul-10 23:05 
GeneralRe: My vote of 3 [modified] Pin
x.infiniteloop27-Jul-10 10:04
memberx.infiniteloop27-Jul-10 10:04 
Questionhow can i create relation between two tables for my jdbc project? Pin
nurhussen25-May-10 20:43
membernurhussen25-May-10 20:43 
AnswerRe: how can i create relation between two tables for my jdbc project? Pin
x.infiniteloop27-Jul-10 10:01
memberx.infiniteloop27-Jul-10 10:01 
Generalthanks Pin
hanhealthy23-Dec-09 7:20
memberhanhealthy23-Dec-09 7:20 
GeneralRe: thanks Pin
x.infiniteloop6-Mar-10 0:20
memberx.infiniteloop6-Mar-10 0:20 
Generalthax Pin
srishadonkeymonkey13-Nov-09 6:33
membersrishadonkeymonkey13-Nov-09 6:33 
GeneralRe: thax Pin
x.infiniteloop6-Mar-10 0:19
memberx.infiniteloop6-Mar-10 0:19 
General[My vote of 2] Wrong Name chosen for the Article Pin
Bunty198325-Sep-09 16:52
memberBunty198325-Sep-09 16:52 
GeneralRe: [My vote of 2] Wrong Name chosen for the Article Pin
x.infiniteloop6-Mar-10 0:09
memberx.infiniteloop6-Mar-10 0:09 
QuestionNot working... Pin
*~~Vettaikaran~~*16-Apr-09 1:29
member*~~Vettaikaran~~*16-Apr-09 1:29 
GeneralAbout Application Pin
Silent_Developer26-Nov-08 1:43
memberSilent_Developer26-Nov-08 1:43 
GeneralRe: About Application Pin
x.infiniteloop4-Dec-08 7:00
memberx.infiniteloop4-Dec-08 7:00 
GeneralRe: About Application Pin
x.infiniteloop4-Dec-08 7:39
memberx.infiniteloop4-Dec-08 7:39 
Questionquestion Pin
sivakrishna.g25-Nov-08 18:31
membersivakrishna.g25-Nov-08 18:31 
AnswerRe: question Pin
x.infiniteloop4-Dec-08 6:39
memberx.infiniteloop4-Dec-08 6:39 
AnswerRe: question Pin
x.infiniteloop4-Dec-08 7:40
memberx.infiniteloop4-Dec-08 7:40 
GeneralStill not working... Pin
Disha0219-Nov-08 7:42
memberDisha0219-Nov-08 7:42 
GeneralRe: Still not working... [modified] Pin
x.infiniteloop23-Nov-08 1:39
memberx.infiniteloop23-Nov-08 1:39 
GeneralRe: Still not working... Pin
Ameya_8717-Dec-08 5:45
memberAmeya_8717-Dec-08 5:45 
GeneralRe: Still not working... Pin
x.infiniteloop17-Dec-08 7:50
memberx.infiniteloop17-Dec-08 7:50 
GeneralRe: its working... Pin
Prasadsd8-Mar-09 7:55
memberPrasadsd8-Mar-09 7:55 
QuestionNIce Code..need help?? Pin
Disha0217-Nov-08 8:26
memberDisha0217-Nov-08 8:26 
AnswerRe: NIce Code..need help?? Pin
haivh8618-Nov-08 19:47
memberhaivh8618-Nov-08 19:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 3 Apr 2009
Article Copyright 2008 by Akshay Bapat
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid