Click here to Skip to main content
13,895,213 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


64 bookmarked
Posted 8 Jan 2013
Licenced CPOL

A Java Chat Application

, 11 Sep 2013
Rate this:
Please Sign up or sign in to vote.
A chat application which can handle multiple users at the same time.

Sample Image


In this article I am demonstrating a chat application which can handle multiple users at the same time. It also supports file transfer.

It is entirely based on Java and consists of two parts: jMessenger (client application) and jServer (server application).


  1. Handles multiple users at the same time
  2. Support for both public and private messages
  3. User signup and login available 
  4. Support for file transfer

Using the code

Run the jar files jMessenger.jar and jServer.jar and do the following:

  1. On jServer select "data.xml" as database file. This file contains usernames and passwords.
  2. On jMessenger select "History.xml" as history file. This file is used to save chat history.
  3. In many cases, if jMessenger cannot find the server then adjust firewall to give it network access.

Both applications are written in Netbeans and you can import source files in Netbeans to view and edit them.

Message structure

Each message in jMessenger has four fields:

  • type: This can be set to message, login, newuser, etc.
  • sender: The username of sender
  • content: Actual content of the message
  • Recipient: Username of recipient of the message


There are two main classes in jServer for handling connections and messages. On startup the SocketServer runs in a separate thread. The job of SocketServer is to wait for connections and for each connection start a new thread ServerThread. Once the connection is established, ServerThread will listen for any messages and hand it over to SocketServer to process. Also it will forward messages from other users to the connected user.

// In ServerThread read the incoming message and hand it to SocketServer

Message msg = (Message) streamIn.readObject();
server.handle(ID, msg);

// In SocketServer process the messages based on their type

public synchronized void handle(int ID, Message msg){  
	else id(msg.type.equals("message")){
		if(msg.recipient.equals("All")){ Announce("message", msg.sender, msg.content); }
			// Find the thread of recipient and forward it to him


jMessenger first connects to the jServer, specified by its IP-address and port number. Arriving messages are then displayed on message board along with their senders.

When a user wants to send a file, first his request is sent via a message of type upload_req. The recipient then does the following:

  1. The recipient side sends its reply in a message of type upload_res
  2. If request is accepted then the recipient opens a new port
  3. For positive reply, recipient's IP address and port number is sent back
  4. The sender, on receiving positive reply connects to this socket and starts file upload

An advantage of this approach is that the clients can chat and transfer files at the same time. Unlike messages, files do not go through jServer.

// On recipient side, start a new thread for download

Download dwn = new Download(....);
Thread t = new Thread(dwn);
send(new Message("upload_res", ui.username, dwn.port, msg.sender));
// Reply to sender with IP address and port number

// On sender side, start a new thread for file upload

// Connect to the port specified in reply
Upload upl = new Upload(addr, port, ui.file, ui);
Thread t = new Thread(upl);


There was much confusion about two issues regarding the project. I would like to clarify that here. 

1. Chat History is not complete. The project's main purpose was to demonstrate networking concepts and due to deadline limitation it was not completed.

2. Many people are confused why chat over different networks is not possible. To understand this, take the example of any web-server. For any browser to connect to a web-server, this server needs to have a global IP address so that it is visible on the Internet. Similarly jServer also is a application server and for chat over two different networks (say a campus LAN and DSL at your house), it also need to be run on a computer with a global IP address. 

About this project 

This is a class project which I wanted to share with others. Source code is licensed under The Code Project Open License (CPOL). The purpose of this project was to demonstrate Java sockets, so the whole History feature is not implemented as good as I would have liked. If this article proves to be any help, please let me know.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anurag Saini
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionPLS HELP!!! Pin
Member 1319451712-May-17 18:03
memberMember 1319451712-May-17 18:03 
Questionnot able to connect to host Pin
Member 1315593527-Apr-17 0:19
memberMember 1315593527-Apr-17 0:19 
Questionchanging package names Pin
Member 1314188922-Apr-17 11:05
memberMember 1314188922-Apr-17 11:05 
Questionerror Pin
Member 1312966721-Apr-17 8:23
memberMember 1312966721-Apr-17 8:23 
QuestionGreat... Pin
Fabricio Parra13-Jan-17 17:43
memberFabricio Parra13-Jan-17 17:43 
QuestionChat Application Pin
Member 1239349917-Sep-16 22:41
memberMember 1239349917-Sep-16 22:41 
Questionregarding computers connecting over a network Pin
Member 1269129919-Aug-16 5:46
memberMember 1269129919-Aug-16 5:46 
AnswerRe: regarding computers connecting over a network Pin
Member 128350397-Jul-17 3:53
memberMember 128350397-Jul-17 3:53 
QuestionHow to connect.... Pin
S.Shrestha5-Aug-16 3:29
memberS.Shrestha5-Aug-16 3:29 
QuestionRun on a vserver Pin
Member 1239643629-Jul-16 3:53
memberMember 1239643629-Jul-16 3:53 
QuestionTCP/UDP Pin
Member 1246746717-Apr-16 23:57
memberMember 1246746717-Apr-16 23:57 
AnswerRe: TCP/UDP Pin
Member 1351481910-Nov-17 11:27
memberMember 1351481910-Nov-17 11:27 
QuestionRegarding chat Pin
Member 1169767817-Apr-16 23:28
memberMember 1169767817-Apr-16 23:28 
Questionrunning this program Pin
Member 124341193-Apr-16 13:43
memberMember 124341193-Apr-16 13:43 
QuestionRegarding file transfer over lan Pin
Member 1206533028-Mar-16 14:56
memberMember 1206533028-Mar-16 14:56 
Questionhow to enhance it by compressing those upload file? Pin
Member 1212533624-Nov-15 1:00
memberMember 1212533624-Nov-15 1:00 
Questionchat app connection with other computers Pin
Member 121255129-Nov-15 5:26
memberMember 121255129-Nov-15 5:26 
Questionsource code compile error Pin
Syed Habib Ullah Shah20-Oct-15 4:32
memberSyed Habib Ullah Shah20-Oct-15 4:32 
GeneralMy vote of 5 Pin
Member 119728619-Sep-15 20:23
memberMember 119728619-Sep-15 20:23 
Questionchat app Pin
Member 1186923528-Jul-15 5:32
memberMember 1186923528-Jul-15 5:32 
Questionamazing dude........... Pin
Member 1185403528-Jul-15 1:22
memberMember 1185403528-Jul-15 1:22 
QuestionA Question (Commands) Pin
Itachai30-Jun-15 9:35
memberItachai30-Jun-15 9:35 
QuestionOh! nice app Pin
Member 1167652213-May-15 20:20
memberMember 1167652213-May-15 20:20 
Questionto run the code Pin
Member 1165177129-Apr-15 7:56
memberMember 1165177129-Apr-15 7:56 
Questionjava chat application Pin
Member 1152932318-Mar-15 3:36
memberMember 1152932318-Mar-15 3:36 

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 | Cookies | Terms of Use | Mobile
Web06 | 2.8.190306.1 | Last Updated 11 Sep 2013
Article Copyright 2013 by Anurag Saini
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid