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

A Java Chat Application

By , 8 Jan 2013
 

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. Clients can manage chat history
  5. 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();

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)

About the Author

Anurag Saini
Student
India India
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow is the jList1 loaded with active usersmemberIsuruSam1-Jun-13 22:58 
AnswerRe: How is the jList1 loaded with active usersmemberAnurag Saini4-Jun-13 18:41 
GeneralMy vote of 4memberIsuruSam1-Jun-13 22:36 
Questionhow to connect using vmwarememberMember 99892614-May-13 18:28 
AnswerRe: how to connect using vmwarememberAnurag Saini12-May-13 19:56 
QuestionExternal connectionmemberPete_12324-Apr-13 0:11 
AnswerRe: External connectionmemberAnurag Saini24-Apr-13 5:08 
Questionchatting project in javamemberayush akarsh12-Apr-13 19:13 
AnswerRe: chatting project in javamemberAnurag Saini20-Apr-13 2:42 
Questionrelated to this projectmemberMember 97888274-Apr-13 6:39 
AnswerRe: related to this projectmemberAnurag Saini20-Apr-13 3:02 
Questionjava multi threadmembernasrin seifi1-Apr-13 1:16 
Questionrelated to this project [modified]memberMember 978882730-Mar-13 3:09 
Questionrelated to this projectmemberMember 97888275-Mar-13 2:44 
Questionrelated to this projectmemberMember 97888274-Mar-13 0:22 
AnswerRe: related to this projectmemberAnurag Saini4-Mar-13 4:39 
QuestionRelated to this project [modified]memberMember 978882721-Feb-13 23:04 
AnswerRe: Related to this projectmemberAnurag Saini23-Feb-13 0:00 
QuestionRelated to this projectmemberMember 978882721-Feb-13 15:10 
AnswerRe: Related to this projectmemberAnurag Saini21-Feb-13 17:23 
Questionrelated to this project [modified]memberMember 978882721-Feb-13 0:48 
Questionrelated to this project [modified]memberMember 978882720-Feb-13 15:00 
AnswerRe: related to this projectmemberAnurag Saini20-Feb-13 23:31 
Questionhow to run this projectmemberMember 978882720-Feb-13 6:21 
AnswerRe: how to run this projectmemberAnurag Saini20-Feb-13 6:42 
QuestionBefore I vote I have some comments.mvpMarcus Kramer16-Jan-13 9:31 
GeneralMy vote of 4memberPhat (Phillip) H. VU8-Jan-13 16:34 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 8 Jan 2013
Article Copyright 2013 by Anurag Saini
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid