📄 chatserver.java
字号:
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
/*****************Chat Server ********************************************/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************/
package com.jeeva.chatserver;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.util.Properties;
import java.io.DataOutputStream;
import java.io.Serializable;
import java.io.InputStream;
import java.util.ArrayList;
import java.awt.Button;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Frame;
import java.awt.Color;
import java.awt.Label;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowListener;
import java.awt.BorderLayout;
public class ChatServer extends Frame implements Serializable, ActionListener,Runnable,CommonSettings {
public ChatServer() {
/*******Intialize all components*********/
this.setTitle("Chat Server");
Image iconImage = Toolkit.getDefaultToolkit().getImage("images/icon.gif");
this.setIconImage(iconImage);
this.setResizable(false);
this.setBackground(Color.yellow);
this.setLayout(new BorderLayout());
/********Top Panel Coding*********/
Panel topPanel = new Panel(new BorderLayout());
topPanel.setBackground(Color.black);
Label lblTitle = new Label("CHAT SERVER V1.0",1);
lblTitle.setForeground(Color.white);
lblTitle.setFont(new Font("Helvitica",Font.BOLD,20));
topPanel.add("Center",lblTitle);
add("North",topPanel);
/********Center Panel Coding*********/
Panel centerPanel = new Panel(null);
cmdStart = new Button("START SERVER");
cmdStart.setBounds(125,10,150,30);
cmdStart.addActionListener(this);
centerPanel.add(cmdStart);
cmdStop = new Button("STOP SERVER");
cmdStop.setBounds(125,50,150,30);
cmdStop.setEnabled(false);
cmdStop.addActionListener(this);
centerPanel.add(cmdStop);
add("Center",centerPanel);
setSize(400,150);
show();
/*****Window Closing Event Section*******/
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitServer();
dispose();
System.exit(0);
}
});
}
/*********Action Listener Coding Starts*************/
public void actionPerformed(ActionEvent evt)
{
if(evt.getActionCommand().equalsIgnoreCase("Start Server"))
{
DBProperties = GetDBProperties();
/*********Initialize the Server Socket*********/
try {
RoomList = "";
if(DBProperties.getProperty("roomlist") != null)
{
RoomList = DBProperties.getProperty("roomlist");
}
int m_portNo = 1436;
if(DBProperties.getProperty("portno") != null)
m_portNo = Integer.parseInt(DBProperties.getProperty("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);
thread.start();
cmdStart.setEnabled(false);
cmdStop.setEnabled(true);
}
if(evt.getActionCommand().equalsIgnoreCase("Stop Server"))
{
ExitServer();
cmdStop.setEnabled(false);
cmdStart.setEnabled(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 = serversocket.accept();
/******* Create a Seperate Thread for that each client**************/
chatcommunication = new ChatCommunication(this,socket);
thread.sleep(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(clientsocket.getOutputStream());
dataoutputstream.write(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 = userarraylist.size();
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
TempClientObject = (ClientObject) userarraylist.get(G_ILoop);
if(TempClientObject.getUserName().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 = userarraylist.size();
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
clientobject = (ClientObject) userarraylist.get(G_ILoop);
if(clientobject.getUserName().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 = userarraylist.size();
String m_addRFC = "ADD "+UserName;
StringBuffer stringbuffer = new StringBuffer("LIST ");
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
clientobject = (ClientObject) userarraylist.get(G_ILoop);
/***Check the Room Name*****/
if(clientobject.getRoomName().equals(ROOM_NAME))
{
SendMessageToClient(clientobject.getSocket(),m_addRFC);
stringbuffer.append(clientobject.getUserName());
stringbuffer.append(";");
}
}
/*****Add a user in to array list***/
clientobject = new ClientObject(ClientSocket,UserName,ROOM_NAME);
userarraylist.add(clientobject);
/********Sending the Complte User List to the New User***********/
stringbuffer.append(UserName);
stringbuffer.append(";");
SendMessageToClient(ClientSocket,stringbuffer.toString());
}
/**********Function to Remove User From Server**************/
public void RemoveUser(String UserName, String RoomName, int RemoveType)
{
ClientObject removeclientobject = GetClientObject(UserName);
if(removeclientobject != null)
{
userarraylist.remove(removeclientobject);
userarraylist.trimToSize();
int m_userListSize = userarraylist.size();
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) userarraylist.get(G_ILoop);
if(clientobject.getRoomName().equals(RoomName))
SendMessageToClient(clientobject.getSocket(),m_RemoveRFC);
}
}
}
/**********Remove User When Exception Occurs **************/
protected void RemoveUserWhenException(Socket clientsocket)
{
int m_userListSize = userarraylist.size();
ClientObject removeclientobject;
for(G_ILoop = 0; G_ILoop < m_userListSize; G_ILoop++)
{
removeclientobject = (ClientObject) userarraylist.get(G_ILoop);
if(removeclientobject.getSocket().equals(clientsocket))
{
String m_RemoveUserName = removeclientobject.getUserName();
String m_RemoveRoomName = removeclientobject.getRoomName();
userarraylist.remove(removeclientobject);
userarraylist.trimToSize();
m_userListSize = userarraylist.size();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -