Server :
Chat Communication :
/*****************Chat Server Client Communcation Object******************/
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ChatCommunication implements Runnable,CommonSettings
/********Initialize the Socket to the Client***********/
ChatCommunication(ChatServer chatserver,Socket clientsocket)
Parent = chatserver;
socket = clientsocket;
try {
inputstream = new DataInputStream(new
BufferedInputStream([Link]()));
}catch(IOException _IOExc) { }
thread = new Thread(this);
[Link]();
/********Implement the Thread Interface*********/
public void run()
while(thread != null)
try {
RFC = [Link]();
/*******RFC Checking**************/
if([Link]("HELO"))
[Link](socket,[Link](5));
if([Link]("QUIT"))
[Link]([Link](5,[Link]("~")),[Link]([Link]("~")
+1),REMOVE_USER);
QuitConnection();
if([Link]("KICK"))
[Link]([Link](5,[Link]("~")),[Link]([Link]("~")
+1),KICK_USER);
QuitConnection();
}
if([Link]("CHRO"))
[Link](socket,[Link](5,[Link]("~")),[Link]([Link]("~")
+1));
if([Link]("MESS"))
[Link](socket,[Link]([Link](":")
+1),[Link]([Link]("~")+1,[Link](":")),[Link](5,[Link]("~")));
if([Link]("PRIV"))
[Link]([Link]([Link]("~")
+1),[Link](5,[Link]("~")));
if([Link]("ROCO"))
[Link](socket,[Link](5));
if([Link]("CALL"))
{
[Link](socket,[Link](5,[Link]("~")),[Link]([Link]("~")
+1));
if([Link]("ACCE"))
[Link](socket,[Link](5,[Link]("~")),[Link]([Link]("~")+1));
if([Link]("CANC"))
[Link]([Link](5,[Link]("~")),[Link]([Link]("~")+1));
if([Link]("QVCT"))
[Link]([Link](5,[Link]("~")),[Link]([Link]("~")+1));
if([Link]("REIP"))
{
[Link](socket,[Link](5,[Link]("~")),[Link]([Link]("~")
+1));
if([Link]("AEIP"))
[Link](socket,[Link](5,[Link]("~")),[Link]([Link]("~
")+1));
if([Link]("QUVC"))
[Link]([Link](5));
}catch(Exception _Exc)
{ [Link](socket);QuitConnection();}
private void QuitConnection()
[Link]();
thread = null;
try {
[Link]();
}catch(IOException _IOExc) { }
socket = null;
/**********Global Variable Declarations***************/
Thread thread;
Socket socket;
DataInputStream inputstream;
String RFC;
ChatServer Parent;
Chat Server :
/*****************Chat Server ********************************************/
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ChatServer extends Frame implements Serializable,
ActionListener,Runnable,CommonSettings {
public ChatServer() {
/*******Intialize all components*********/
[Link]("Chat Server");
Image iconImage = [Link]().getImage("images/[Link]");
[Link](iconImage);
[Link](false);
[Link]([Link]);
[Link](new BorderLayout());
/********Top Panel Coding*********/
Panel topPanel = new Panel(new BorderLayout());
[Link]([Link]);
Label lblTitle = new Label("CHAT SERVER V1.0",1);
[Link]([Link]);
[Link](new Font("Helvitica",[Link],20));
[Link]("Center",lblTitle);
add("North",topPanel);
/********Center Panel Coding*********/
Panel centerPanel = new Panel(null);
cmdStart = new Button("START SERVER");
[Link](125,10,150,30);
[Link](this);
[Link](cmdStart);
cmdStop = new Button("STOP SERVER");
[Link](125,50,150,30);
[Link](false);
[Link](this);
[Link](cmdStop);
add("Center",centerPanel);
setSize(400,150);
show();
/*****Window Closing Event Section*******/
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitServer();
dispose();
[Link](0);
});
/*********Action Listener Coding Starts*************/
public void actionPerformed(ActionEvent evt)
if([Link]().equalsIgnoreCase("Start Server"))
DBProperties = GetDBProperties();
/*********Initialize the Server Socket*********/
try {
RoomList = "";
if([Link]("roomlist") != null)
RoomList =
[Link]("roomlist");
}
int m_portNo = 1436;
if([Link]("portno") != null)
m_portNo =
[Link]([Link]("portno"));
serversocket = new ServerSocket(m_portNo);
}catch(IOException _IOExc) { }
/********Initialize the Array List**********/
userarraylist = new ArrayList();
messagearraylist = new ArrayList();
/********Initialize the thread*************/
thread = new Thread(this);
[Link]();
[Link](false);
[Link](true);
if([Link]().equalsIgnoreCase("Stop Server"))
ExitServer();
[Link](false);
[Link](true);
}
}
/*************Thread Implementation***************/
public void run()
/*********Accepting all the client connections and create a seperate thread******/
while(thread != null)
try
/********Accepting the Server Connections***********/
socket = [Link]();
/******* Create a Seperate Thread for that each
client**************/
chatcommunication = new ChatCommunication(this,socket);
[Link](THREAD_SLEEP_TIME);
catch(InterruptedException _INExc) { ExitServer(); }
catch(IOException _IOExc) { ExitServer(); }
/***** Function To Send a Message to Client **********/
private void SendMessageToClient(Socket clientsocket,String message)
{
try {
dataoutputstream = new
DataOutputStream([Link]());
[Link](new String(message+"\r\n").getBytes());
}catch(IOException _IOExc) { }
/*********Function To Get the Object Of Given User Name*********/
private ClientObject GetClientObject(String UserName)
ClientObject returnClientObject = null;
ClientObject TempClientObject;
int m_userListSize = [Link]();
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
TempClientObject = (ClientObject) [Link](G_ILoop);
if([Link]().equalsIgnoreCase(UserName))
returnClientObject = TempClientObject;
break;
return returnClientObject;
/*****Function To Check whether the Username is Already Exists**********/
private boolean IsUserExists(String UserName)
if(GetClientObject(UserName) != null)
return true;
else
return false;
/***********Function to get the Index of specified User Name********/
private int GetIndexOf(String UserName)
int m_userListSize = [Link]();
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
if([Link]().equalsIgnoreCase(UserName))
return G_ILoop;
return -1;
/********Function To Add a New Client in to the Server List**********/
protected void AddUser(Socket ClientSocket,String UserName)
/***If User name Exists return**/
if(IsUserExists(UserName))
SendMessageToClient(ClientSocket,"EXIS");
return;
/********Send a Room List ********/
SendMessageToClient(ClientSocket,"ROOM "+RoomList);
/********Send the New User Detail into All Other Users****/
int m_userListSize = [Link]();
String m_addRFC = "ADD "+UserName;
StringBuffer stringbuffer = new StringBuffer("LIST ");
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
/***Check the Room Name*****/
if([Link]().equals(ROOM_NAME))
SendMessageToClient([Link](),m_addRFC);
[Link]([Link]());
[Link](";");
}
}
/*****Add a user in to array list***/
clientobject = new ClientObject(ClientSocket,UserName,ROOM_NAME);
[Link](clientobject);
/********Sending the Complte User List to the New User***********/
[Link](UserName);
[Link](";");
SendMessageToClient(ClientSocket,[Link]());
/**********Function to Remove User From Server**************/
public void RemoveUser(String UserName, String RoomName, int RemoveType)
ClientObject removeclientobject = GetClientObject(UserName);
if(removeclientobject != null)
[Link](removeclientobject);
[Link]();
int m_userListSize = [Link]();
String m_RemoveRFC=null;
if(RemoveType == REMOVE_USER)
m_RemoveRFC = "REMO "+UserName;
if(RemoveType == KICK_USER)
m_RemoveRFC = "INKI "+UserName;
/*****Send a REMO RFC to all other Users****/
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
if([Link]().equals(RoomName))
SendMessageToClient([Link](),m_RemoveRFC);
/**********Remove User When Exception Occurs **************/
protected void RemoveUserWhenException(Socket clientsocket)
int m_userListSize = [Link]();
ClientObject removeclientobject;
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
removeclientobject = (ClientObject) [Link](G_ILoop);
if([Link]().equals(clientsocket))
String m_RemoveUserName =
[Link]();
String m_RemoveRoomName =
[Link]();
[Link](removeclientobject);
[Link]();
m_userListSize = [Link]();
String m_RemoveRFC="REMO "+m_RemoveUserName;
/*****Send a REMO RFC to all other Users****/
for(int m_ILoop = 0; m_ILoop < m_userListSize; m_ILoop++)
clientobject = (ClientObject)
[Link](m_ILoop);
if([Link]().equals(m_RemoveRoomName))
SendMessageToClient([Link](),m_RemoveRFC);
return;
/*********Function To Change the Room *****************/
public void ChangeRoom(Socket ClientSocket,String UserName, String NewRoomName)
int m_clientIndex = GetIndexOf(UserName);
if(m_clientIndex >= 0)
/********Update the Old Room to New Room and send the RFC**********/
ClientObject TempClientObject = (ClientObject) [Link](m_clientIndex);
String m_oldRoomName = [Link]();
[Link](NewRoomName);
[Link](m_clientIndex,TempClientObject);
SendMessageToClient(ClientSocket,"CHRO "+NewRoomName);
/****Send all the Users list of that particular room to that client socket****/
int m_userListSize = [Link]();
StringBuffer stringbuffer = new StringBuffer("LIST ");
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
/***Check the Room Name*****/
if([Link]().equals(NewRoomName))
[Link]([Link]());
[Link](";");
SendMessageToClient(ClientSocket,[Link]());
/**********Inform to Old Room and New Room Users**********/
String m_OldRoomRFC = "LERO "+UserName+"~"+NewRoomName;
String m_NewRoomRFC = "JORO "+UserName;
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
if([Link]().equals(m_oldRoomName))
SendMessageToClient([Link](),m_OldRoomRFC);
if(([Link]().equals(NewRoomName)) && (!
([Link]().equals(UserName))))
SendMessageToClient([Link](),m_NewRoomRFC);
/********Function to Send General Message ***************/
protected void SendGeneralMessage(Socket ClientSocket, String Message,String
UserName,String RoomName)
boolean m_floodFlag = false;
[Link](UserName);
if([Link]() > MAX_MESSAGE)
{
[Link](0);
[Link]();
/*********Chk Whether the User is flooding the message*********/
String m_firstMessage = (String) [Link](0);
int m_messageListSize = [Link]();
for(G_ILoop = 1; G_ILoop < m_messageListSize; G_ILoop++)
if([Link](G_ILoop).equals(m_firstMessage))
m_floodFlag = true;
else
m_floodFlag = false;
break;
/********Sending a General Message to All the Users*******/
int m_userListSize = [Link]();
String m_messageRFC = "MESS "+UserName+":"+Message;
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
clientobject = (ClientObject) [Link](G_ILoop);
if(([Link]().equals(RoomName)) && (!
([Link]().equals(UserName))))
SendMessageToClient([Link](),m_messageRFC);
/********Kick Off the User If he/she flooding the message********/
if(m_floodFlag)
SendMessageToClient(ClientSocket,"KICK ");
[Link]();
/*************Function To Send Private Message *************/
protected void SendPrivateMessage(String Message , String ToUserName)
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"PRIV "+Message);
}
}
/*************Function To Request User For Voice Chat *************/
protected void RequestForVoiceChat(Socket ClientSocket,String FromUserName,String
ToUserName)
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"REQU "+
GetClientObject(FromUserName).getSocket().getInetAddress().getHostAddress() + "~" +
FromUserName);
/*************Function To Quit Voice Chat *************/
protected void QuitVoiceChat(String FromUserName,String ToUserName)
[Link](FromUserName + "-->" + ToUserName);
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"QVCT "+ FromUserName + "~" +
ToUserName);
}
}
/*************Function To Send User IP For Voice Chat *************/
protected void SendUserIP(Socket ClientSocket,String FromUserName,String ToUserName)
clientobject = GetClientObject(ToUserName);
if(ClientSocket != null)
SendMessageToClient([Link](),"ADDR "+
GetClientObject(FromUserName).getSocket().getInetAddress().getHostAddress() + "~" +
FromUserName);
/*************Function To Reject The Request For Voice Chat *************/
protected void RejectCall(String FromUserName,String ToUserName)
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"REJC "+ FromUserName + "~" +
ToUserName);
}
}
/***********Function to Get Remote User Address ******************/
protected void GetRemoteUserAddress(Socket ClientSocket,String ToUserName, String
FromUserName)
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"REIP "+ FromUserName
+"~"+[Link]().getHostAddress());
/***********Function to Get Remote User Address ******************/
protected void SendRemoteUserAddress(Socket ClientSocket,String ToUserName, String
FromUserName)
clientobject = GetClientObject(FromUserName);
if(clientobject != null)
SendMessageToClient([Link](),"AEIP "+ ToUserName
+"~"+[Link]().getHostAddress());
}
/***********Function to Quit Video Chat ******************/
protected void QuitVideoChat(String ToUserName)
clientobject = GetClientObject(ToUserName);
if(clientobject != null)
SendMessageToClient([Link](),"QUVC");
/*********Function to get the User Count in the Room***********/
protected void GetUserCount(Socket ClientSocket,String RoomName)
int m_userListSize = [Link]();
int m_userCount = 0;
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
clientobject = (ClientObject) [Link](G_ILoop);
if([Link]().equals(RoomName))
m_userCount++;
SendMessageToClient(ClientSocket,"ROCO "+RoomName+"~"+m_userCount);
}
/***********Function to Destroy the Objects***********/
private void ExitServer()
if(thread != null)
[Link]();
thread = null;
try {
if(serversocket != null)
[Link]();
serversocket = null;
}catch(IOException _IOExc) { }
userarraylist = null;
messagearraylist = null;
[Link](false);
[Link](true);
/*********Loading Properties File*******************/
private Properties GetDBProperties()
/****************** Getting the Property Value From Propeyty File
************************/
Properties DBProperties = new Properties();
try
InputStream inputstream =
[Link]().getClassLoader().getResourceAsStream("[Link]");
[Link](inputstream);
[Link]();
catch (IOException _IOExc){ }
finally
return (DBProperties);
public static void main(String[] args) {
ChatServer mainFrame = new ChatServer();
[Link](true);
/********Global Variable Declarations***********/
Properties DBProperties;
Button cmdStart,cmdStop;
ServerSocket serversocket;
Socket socket;
ArrayList userarraylist,messagearraylist;
Thread thread;
ChatCommunication chatcommunication;
DataOutputStream dataoutputstream;
int G_ILoop;
ClientObject clientobject;
String RoomList;
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*****************Chat Server Client Object*******************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
package [Link];
import [Link];
public class ClientObject
ClientObject(Socket socket,String UserName,String RoomName)
ClientSocket = socket;
ClientUserName = UserName;
ClientRoomName = RoomName;
}
public void setSocket(Socket socket)
ClientSocket = socket;
public void setUserName(String UserName)
ClientUserName = UserName;
public void setRoomName(String RoomName)
ClientRoomName = RoomName;
public Socket getSocket()
return ClientSocket;
public String getUserName()
return ClientUserName;
}
public String getRoomName()
return ClientRoomName;
Socket ClientSocket;
String ClientUserName,ClientRoomName;
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*****************Chat Server Constant Settings***************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
package [Link];
public interface CommonSettings
public int THREAD_SLEEP_TIME = 1500;
public int KICK_USER = 0;
public int REMOVE_USER = 1;
public int MAX_MESSAGE = 30;
public String ROOM_NAME = "General";