Click here to Skip to main content
Click here to Skip to main content

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

, 3 Apr 2009 GPL3
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...

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

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

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

License

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

Share

About the Author

Akshay Bapat

India India
No Biography provided

Comments and Discussions

 
Bugbroadcasting that the user has logged out Pinmemberjumanjiarumugam5-Sep-12 4:42 
Questionhai PinmemberMember 78463589-Feb-12 4:54 
QuestionLOGGED out message not broadcasted PinmemberMember 837880129-Nov-11 6:10 
Questionhow to broadcast a message PinmemberMember 837880129-Nov-11 5:35 
Questionmsg to all users PinmemberMember 837880128-Nov-11 14:02 
QuestionModify Chat Pinmembermustafatekin14-Nov-11 21:17 
Questionproject report PinmemberMember 832536714-Nov-11 3:41 
Questionplz.. tell me the concepts behind the project for my viva on this project .. Pinmemberharshaggarwal21-Dec-10 15:38 
GeneralMy vote of 3 Pinmemberxujianguo26-Jul-10 23:05 
GeneralRe: My vote of 3 [modified] Pinmemberx.infiniteloop27-Jul-10 10:04 
Questionhow can i create relation between two tables for my jdbc project? Pinmembernurhussen25-May-10 20:43 
AnswerRe: how can i create relation between two tables for my jdbc project? Pinmemberx.infiniteloop27-Jul-10 10:01 
Generalthanks Pinmemberhanhealthy23-Dec-09 7:20 
GeneralRe: thanks Pinmemberx.infiniteloop6-Mar-10 0:20 
Generalthax Pinmembersrishadonkeymonkey13-Nov-09 6:33 
GeneralRe: thax Pinmemberx.infiniteloop6-Mar-10 0:19 
General[My vote of 2] Wrong Name chosen for the Article PinmemberBunty198325-Sep-09 16:52 
GeneralRe: [My vote of 2] Wrong Name chosen for the Article Pinmemberx.infiniteloop6-Mar-10 0:09 
QuestionNot working... Pinmember*~~Vettaikaran~~*16-Apr-09 1:29 
GeneralAbout Application PinmemberSilent_Developer26-Nov-08 1:43 
GeneralRe: About Application Pinmemberx.infiniteloop4-Dec-08 7:00 
GeneralRe: About Application Pinmemberx.infiniteloop4-Dec-08 7:39 
Questionquestion Pinmembersivakrishna.g25-Nov-08 18:31 
AnswerRe: question Pinmemberx.infiniteloop4-Dec-08 6:39 
AnswerRe: question Pinmemberx.infiniteloop4-Dec-08 7:40 
GeneralStill not working... PinmemberDisha0219-Nov-08 7:42 
GeneralRe: Still not working... [modified] Pinmemberx.infiniteloop23-Nov-08 1:39 
GeneralRe: Still not working... PinmemberAmeya_8717-Dec-08 5:45 
GeneralRe: Still not working... Pinmemberx.infiniteloop17-Dec-08 7:50 
GeneralRe: its working... PinmemberPrasadsd8-Mar-09 7:55 
QuestionNIce Code..need help?? PinmemberDisha0217-Nov-08 8:26 
AnswerRe: NIce Code..need help?? Pinmemberhaivh8618-Nov-08 19:47 

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

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

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