Click here to Skip to main content
15,886,806 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.6K   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

 
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 
GeneralStill not working... Pin
Disha0219-Nov-08 6:42
Disha0219-Nov-08 6:42 
GeneralRe: Still not working... [modified] Pin
Akshay Bapat23-Nov-08 0:39
Akshay Bapat23-Nov-08 0:39 
Sorry for the late reply... My machine betrayed me!!!
Here is your explaination... Also, I've edited the article and it's now complete... Check that out...

Well, you first need to set up a server...

The process is too simple...
Run InfiniteChatServer.java and just press start server (No need to edit port no.) ...

Now open another command line window and run InfiniteChatClient.java.
A login form will open.
Enter a username...
In place of servername type : localhost
and hit log in... You'll login...

This way you can open any number of command lines and create many users on the same machine...


If you are on a network, create a server on a machine, get it's IP address(ipconfig) or name(Right-click on My Computer->Properties->Computer name). Then run the client side on another machine in the network. Give a username.
In place of servername, enter the name/IP of the machine where server is running... Log in... It's that simple...



It's quite handy in the labs to chat quietly with friends while working or to copy friend's program when you are bored of typing or when you don't know how to do the program... HEHEHE... Big Grin | :-D Poke tongue | ;-P


P.S.: For details about the errors occuring, I suggest you to check messages in command line... There you'll get status about everything that's going on with the application..

I want to change the world, but god won't share the heckin' source code !!!

modified on Sunday, November 23, 2008 6:45 AM

GeneralRe: Still not working... Pin
Ameya_8717-Dec-08 4:45
Ameya_8717-Dec-08 4:45 
GeneralRe: Still not working... Pin
Akshay Bapat17-Dec-08 6:50
Akshay Bapat17-Dec-08 6:50 
GeneralRe: its working... Pin
Prasadsd8-Mar-09 6:55
Prasadsd8-Mar-09 6:55 
QuestionNIce Code..need help?? Pin
Disha0217-Nov-08 7:26
Disha0217-Nov-08 7:26 
AnswerRe: NIce Code..need help?? Pin
haivh8618-Nov-08 18:47
haivh8618-Nov-08 18: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.