Click here to Skip to main content
12,070,351 members (68,777 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


41 bookmarked

A Java Chat Application

, 11 Sep 2013 CPOL
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

Questionhow to enhance it by compressing those upload file? Pin
Member 1212533624-Nov-15 2:00
memberMember 1212533624-Nov-15 2:00 
Questionchat app connection with other computers Pin
Member 121255129-Nov-15 6:26
memberMember 121255129-Nov-15 6:26 
Questionsource code compile error Pin
Syed Habib Ullah Shah20-Oct-15 5:32
memberSyed Habib Ullah Shah20-Oct-15 5:32 
GeneralMy vote of 5 Pin
Member 119728619-Sep-15 21:23
memberMember 119728619-Sep-15 21:23 
Questionchat app Pin
Member 1186923528-Jul-15 6:32
memberMember 1186923528-Jul-15 6:32 
Questionamazing dude........... Pin
Member 1185403528-Jul-15 2:22
memberMember 1185403528-Jul-15 2:22 
QuestionA Question (Commands) Pin
Member 1180381130-Jun-15 10:35
memberMember 1180381130-Jun-15 10:35 
QuestionOh! nice app Pin
Member 1167652213-May-15 21:20
memberMember 1167652213-May-15 21:20 
Questionto run the code Pin
Member 1165177129-Apr-15 8:56
memberMember 1165177129-Apr-15 8:56 
Questionjava chat application Pin
Member 1152932318-Mar-15 4:36
memberMember 1152932318-Mar-15 4:36 
AnswerRe: java chat application Pin
Member 117547369-Jun-15 20:55
memberMember 117547369-Jun-15 20:55 
QuestionAbout running the code Pin
Member 114503898-Mar-15 7:45
memberMember 114503898-Mar-15 7:45 
QuestionChat Server Pin
Member 114994934-Mar-15 17:44
memberMember 114994934-Mar-15 17:44 
Questionhelp urgent Pin
Member 113532443-Mar-15 5:38
memberMember 113532443-Mar-15 5:38 
QuestionUML diagram Pin
Member 1147509126-Feb-15 8:25
memberMember 1147509126-Feb-15 8:25 
BugProblem with FILE TRANSFER!! :( Pin
Member 1127954526-Feb-15 1:26
memberMember 1127954526-Feb-15 1:26 
Questionunable to run Pin
Member 1122456925-Feb-15 20:35
memberMember 1122456925-Feb-15 20:35 
Questionunable to browse history file Pin
Member 1121270124-Feb-15 19:53
memberMember 1121270124-Feb-15 19:53 
QuestionHelp Pin
Member 1137485013-Feb-15 11:17
memberMember 1137485013-Feb-15 11:17 
QuestionMultiGroup Application Pin
Member 1132421325-Jan-15 20:43
memberMember 1132421325-Jan-15 20:43 
QuestionQuestion? Pin
Member 1140001524-Jan-15 15:30
memberMember 1140001524-Jan-15 15:30 
QuestionAwesome Chat Program Pin
Member 110848258-Dec-14 9:07
memberMember 110848258-Dec-14 9:07 
Questioncant run Pin
Member 112824302-Dec-14 23:47
memberMember 112824302-Dec-14 23:47 
QuestionDo you have a Client code also for Android phone? Pin
Member 1030346630-Nov-14 23:13
professionalMember 1030346630-Nov-14 23:13 
GeneralMy vote of 4 Pin
khadija842123-Nov-14 3:49
memberkhadija842123-Nov-14 3:49 
QuestionConnection Failure Pin
Member 1114912718-Nov-14 1:20
memberMember 1114912718-Nov-14 1:20 
Questionhelpme Pin
Member 1122456911-Nov-14 23:31
memberMember 1122456911-Nov-14 23:31 
QuestionQuestion please Pin
Member 100464094-Nov-14 3:43
memberMember 100464094-Nov-14 3:43 
QuestionGood Application Pin
Mostafa Helmy El-Sayed14-Oct-14 3:33
memberMostafa Helmy El-Sayed14-Oct-14 3:33 
Questiondeploy on internet Pin
Member 109364989-Jul-14 21:30
memberMember 109364989-Jul-14 21:30 
QuestionRequest to anybody Pin
Member 1090140424-Jun-14 8:17
memberMember 1090140424-Jun-14 8:17 
QuestionProtocols/APIs Pin
Member 1090140423-Jun-14 9:08
memberMember 1090140423-Jun-14 9:08 
QuestionThank u.. Pin
Member 1089424418-Jun-14 23:58
memberMember 1089424418-Jun-14 23:58 
Questionhaving problem in locating the data.xml and history.xml files Pin
A.Siddique16-Jun-14 19:18
memberA.Siddique16-Jun-14 19:18 
AnswerRe: having problem in locating the data.xml and history.xml files Pin
Member 1089424419-Jun-14 0:01
memberMember 1089424419-Jun-14 0:01 
Questionnot supporting for wifi lan Pin
Member 107503097-May-14 1:26
memberMember 107503097-May-14 1:26 
Questionserver code Pin
Kritika Sinha 15-May-14 23:44
memberKritika Sinha 15-May-14 23:44 
AnswerRe: server code Pin
Member 1089424419-Jun-14 0:08
memberMember 1089424419-Jun-14 0:08 
QuestionLogin fail Pin
Artur Trofimov4-May-14 23:13
memberArtur Trofimov4-May-14 23:13 
QuestionPlease reply soon Pin
b.maneesh922-May-14 19:46
memberb.maneesh922-May-14 19:46 
Questionjmesenger Pin
Gerrol Castillo26-Apr-14 23:16
memberGerrol Castillo26-Apr-14 23:16 
Questionnetwork Pin
Member 1046055012-Apr-14 5:54
memberMember 1046055012-Apr-14 5:54 
AnswerRe: network Pin
Tumusiime Burt Munro12-Jun-14 23:11
memberTumusiime Burt Munro12-Jun-14 23:11 
how can i make it work in the local area network
QuestionCode Pin
Artur Trofimov11-Apr-14 8:53
memberArtur Trofimov11-Apr-14 8:53 
GeneralGeneral Pin
Kiran Wade5-Apr-14 1:20
memberKiran Wade5-Apr-14 1:20 
Questionhistory file Pin
Member 104605502-Apr-14 11:34
memberMember 104605502-Apr-14 11:34 
AnswerRe: history file Pin
thanhbka8914-Aug-14 21:32
memberthanhbka8914-Aug-14 21:32 
Questionquestion Pin
Member 107193872-Apr-14 9:02
memberMember 107193872-Apr-14 9:02 
Questioncdm Pin
Member 104605502-Apr-14 6:05
memberMember 104605502-Apr-14 6:05 
GeneralThank you :) Pin
Fai ZY30-Mar-14 1:50
memberFai ZY30-Mar-14 1:50 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160208.1 | Last Updated 11 Sep 2013
Article Copyright 2013 by Anurag Saini
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid