Click here to Skip to main content
11,438,007 members (46,793 online)
Click here to Skip to main content

Tagged as

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

Introduction

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

Features

  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

jServer

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){  
	if(msg.type.equals("login")){
		....
	}
	else id(msg.type.equals("message")){
		if(msg.recipient.equals("All")){ Announce("message", msg.sender, msg.content); }
		else{
			// Find the thread of recipient and forward it to him
		}
	}
.......

jMessenger

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);
t.start();
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);
t.start();

Update

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.

License

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

Share

About the Author

Anurag Saini
Student
India India
No Biography provided

Comments and Discussions

 
GeneralThank you :) Pin
Fai ZY30-Mar-14 1:50
memberFai ZY30-Mar-14 1:50 
QuestionConnect Failure Pin
Member 1066209719-Mar-14 14:09
memberMember 1066209719-Mar-14 14:09 
Questionhelp me please urgent Pin
Member 106127032-Mar-14 5:14
memberMember 106127032-Mar-14 5:14 
AnswerRe: help me please urgent Pin
Mattlist29-Mar-14 10:07
memberMattlist29-Mar-14 10:07 
Question2 Questions Pin
Member 1059615017-Feb-14 6:44
memberMember 1059615017-Feb-14 6:44 
Questionhistory file Pin
Member 1050877017-Feb-14 6:04
memberMember 1050877017-Feb-14 6:04 
Questionsend method in SocketClient class Pin
Member 1050877014-Feb-14 18:06
memberMember 1050877014-Feb-14 18:06 
AnswerRe: send method in SocketClient class Pin
Member 1059615018-Feb-14 2:41
memberMember 1059615018-Feb-14 2:41 
Questionmultiplatform systems peer to peer LAN Mesengger Pin
Member 1056715010-Feb-14 18:18
memberMember 1056715010-Feb-14 18:18 
GeneralGreat work Pin
Yogesh Mouli9-Feb-14 6:29
memberYogesh Mouli9-Feb-14 6:29 
GeneralNice application Pin
proton1h110-Jan-14 21:07
memberproton1h110-Jan-14 21:07 
Questionproblem downloading source file Pin
Member 105087707-Jan-14 5:02
memberMember 105087707-Jan-14 5:02 
QuestionThank you :) Pin
Member 1044464929-Dec-13 0:21
memberMember 1044464929-Dec-13 0:21 
Questionfile transfer Pin
Member 104395072-Dec-13 12:21
memberMember 104395072-Dec-13 12:21 
Please the file transfer is not working
Questionnice jop why u donate this Pin
Member 1043461129-Nov-13 7:16
memberMember 1043461129-Nov-13 7:16 
Question. please reply...!! Pin
Member 1034677326-Oct-13 21:54
memberMember 1034677326-Oct-13 21:54 
QuestionMany Thank [modified] Pin
Đức Quỳnh30-Sep-13 0:51
memberĐức Quỳnh30-Sep-13 0:51 
Questioncan't access chat history and recieved file Pin
Member 1026489310-Sep-13 22:13
memberMember 1026489310-Sep-13 22:13 
AnswerRe: can't access chat history and recieved file Pin
Anurag Saini11-Sep-13 0:11
memberAnurag Saini11-Sep-13 0:11 
QuestionSupport for chat over network Pin
mohit godiya5-Sep-13 21:35
membermohit godiya5-Sep-13 21:35 
AnswerRe: Support for chat over network Pin
Anurag Saini11-Sep-13 0:07
memberAnurag Saini11-Sep-13 0:07 
QuestionNice Job Done. Pin
kwusu31-Aug-13 11:03
memberkwusu31-Aug-13 11:03 
AnswerRe: Nice Job Done. Pin
Anurag Saini1-Sep-13 7:41
memberAnurag Saini1-Sep-13 7:41 
GeneralRe: Nice Job Done. Pin
kwusu2-Sep-13 23:22
memberkwusu2-Sep-13 23:22 
QuestionHistory Retrieval Pin
SakshiK27-Jun-13 2:23
memberSakshiK27-Jun-13 2:23 
AnswerRe: History Retrieval Pin
Anurag Saini27-Jun-13 21:22
memberAnurag Saini27-Jun-13 21:22 
QuestionHow is the jList1 loaded with active users Pin
IsuruSam1-Jun-13 23:58
memberIsuruSam1-Jun-13 23:58 
AnswerRe: How is the jList1 loaded with active users Pin
Anurag Saini4-Jun-13 19:41
memberAnurag Saini4-Jun-13 19:41 
GeneralMy vote of 4 Pin
IsuruSam1-Jun-13 23:36
memberIsuruSam1-Jun-13 23:36 
Questionhow to connect using vmware Pin
Member 99892614-May-13 19:28
memberMember 99892614-May-13 19:28 
AnswerRe: how to connect using vmware Pin
Anurag Saini12-May-13 20:56
memberAnurag Saini12-May-13 20:56 
QuestionExternal connection Pin
Pete_12324-Apr-13 1:11
memberPete_12324-Apr-13 1:11 
AnswerRe: External connection Pin
Anurag Saini24-Apr-13 6:08
memberAnurag Saini24-Apr-13 6:08 
GeneralRe: External connection Pin
Member 102539765-Sep-13 0:05
memberMember 102539765-Sep-13 0:05 
Questionchatting project in java Pin
ayush akarsh12-Apr-13 20:13
memberayush akarsh12-Apr-13 20:13 
AnswerRe: chatting project in java Pin
Anurag Saini20-Apr-13 3:42
memberAnurag Saini20-Apr-13 3:42 
Questionrelated to this project Pin
Member 97888274-Apr-13 7:39
memberMember 97888274-Apr-13 7:39 
AnswerRe: related to this project Pin
Anurag Saini20-Apr-13 4:02
memberAnurag Saini20-Apr-13 4:02 
Questionjava multi thread Pin
nasrin seifi1-Apr-13 2:16
membernasrin seifi1-Apr-13 2:16 
Questionrelated to this project [modified] Pin
Member 978882730-Mar-13 4:09
memberMember 978882730-Mar-13 4:09 
Questionrelated to this project Pin
Member 97888275-Mar-13 3:44
memberMember 97888275-Mar-13 3:44 
Questionrelated to this project Pin
Member 97888274-Mar-13 1:22
memberMember 97888274-Mar-13 1:22 
AnswerRe: related to this project Pin
Anurag Saini4-Mar-13 5:39
memberAnurag Saini4-Mar-13 5:39 
QuestionRelated to this project [modified] Pin
Member 978882722-Feb-13 0:04
memberMember 978882722-Feb-13 0:04 
AnswerRe: Related to this project Pin
Anurag Saini23-Feb-13 1:00
memberAnurag Saini23-Feb-13 1:00 
QuestionRelated to this project Pin
Member 978882721-Feb-13 16:10
memberMember 978882721-Feb-13 16:10 
AnswerRe: Related to this project Pin
Anurag Saini21-Feb-13 18:23
memberAnurag Saini21-Feb-13 18:23 
Questionrelated to this project [modified] Pin
Member 978882721-Feb-13 1:48
memberMember 978882721-Feb-13 1:48 
Questionrelated to this project [modified] Pin
Member 978882720-Feb-13 16:00
memberMember 978882720-Feb-13 16:00 
AnswerRe: related to this project Pin
Anurag Saini21-Feb-13 0:31
memberAnurag Saini21-Feb-13 0:31 

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
Web04 | 2.8.150506.1 | Last Updated 11 Sep 2013
Article Copyright 2013 by Anurag Saini
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid