Click here to Skip to main content
15,881,281 members
Articles / Desktop Programming / Swing

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

Rate me:
Please Sign up or sign in to vote.
4.56/5 (16 votes)
3 Apr 2009GPL36 min read 99.5K   9K   52   32
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...

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:

C#
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:

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

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Bugbroadcasting that the user has logged out Pin
jumanjiarumugam5-Sep-12 3:42
jumanjiarumugam5-Sep-12 3:42 
Questionhai Pin
Member 78463589-Feb-12 3:54
Member 78463589-Feb-12 3:54 
QuestionLOGGED out message not broadcasted Pin
Member 837880129-Nov-11 5:10
Member 837880129-Nov-11 5:10 
Questionhow to broadcast a message Pin
Member 837880129-Nov-11 4:35
Member 837880129-Nov-11 4:35 
Questionmsg to all users Pin
Member 837880128-Nov-11 13:02
Member 837880128-Nov-11 13:02 
QuestionModify Chat Pin
mustafatekin14-Nov-11 20:17
mustafatekin14-Nov-11 20:17 
Questionproject report Pin
Member 832536714-Nov-11 2:41
Member 832536714-Nov-11 2:41 
Questionplz.. tell me the concepts behind the project for my viva on this project .. Pin
harshaggarwal21-Dec-10 14:38
harshaggarwal21-Dec-10 14:38 
GeneralMy vote of 3 Pin
xujianguo26-Jul-10 22:05
xujianguo26-Jul-10 22:05 
GeneralRe: My vote of 3 [modified] Pin
Akshay Bapat27-Jul-10 9:04
Akshay Bapat27-Jul-10 9:04 
Questionhow can i create relation between two tables for my jdbc project? Pin
nurhussen25-May-10 19:43
nurhussen25-May-10 19:43 
AnswerRe: how can i create relation between two tables for my jdbc project? Pin
Akshay Bapat27-Jul-10 9:01
Akshay Bapat27-Jul-10 9:01 
Generalthanks Pin
hanhealthy23-Dec-09 6:20
hanhealthy23-Dec-09 6:20 
GeneralRe: thanks Pin
Akshay Bapat5-Mar-10 23:20
Akshay Bapat5-Mar-10 23:20 
Generalthax Pin
srishadonkeymonkey13-Nov-09 5:33
srishadonkeymonkey13-Nov-09 5:33 
GeneralRe: thax Pin
Akshay Bapat5-Mar-10 23:19
Akshay Bapat5-Mar-10 23:19 
General[My vote of 2] Wrong Name chosen for the Article Pin
Bunty198325-Sep-09 15:52
Bunty198325-Sep-09 15:52 
GeneralRe: [My vote of 2] Wrong Name chosen for the Article Pin
Akshay Bapat5-Mar-10 23:09
Akshay Bapat5-Mar-10 23:09 
QuestionNot working... Pin
*~~Vettaikaran~~*16-Apr-09 0:29
*~~Vettaikaran~~*16-Apr-09 0:29 
GeneralAbout Application Pin
Silent_Developer26-Nov-08 0:43
Silent_Developer26-Nov-08 0:43 
GeneralRe: About Application Pin
Akshay Bapat4-Dec-08 6:00
Akshay Bapat4-Dec-08 6:00 
GeneralRe: About Application Pin
Akshay Bapat4-Dec-08 6:39
Akshay Bapat4-Dec-08 6:39 
Questionquestion Pin
sivakrishna.g25-Nov-08 17:31
sivakrishna.g25-Nov-08 17:31 
AnswerRe: question Pin
Akshay Bapat4-Dec-08 5:39
Akshay Bapat4-Dec-08 5:39 
AnswerRe: question Pin
Akshay Bapat4-Dec-08 6:40
Akshay Bapat4-Dec-08 6:40 

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.