📄 chatserver.java
字号:
import javax.swing.*;
//import java.awt.*;
import java.awt.event.*;
//import javax.swing.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer extends Thread implements ActionListener
{
ServerPanel sp=new ServerPanel();
ServerSocket serverSocket;
Socket clientSocket;
ChatMessage msg;
static Vector vector=new Vector(1,1);
static Hashtable h=new Hashtable();
String messageType;
String chatname;
String tochatname;
String chatmsg;
boolean running=false;
public ChatServer()
{
sp.button1.addActionListener(this);
sp.button2.addActionListener(this);
sp.button3.addActionListener(this);
sp.button4.addActionListener(this);
sp.button2.setEnabled(false);
}
public void run()
{
try
{
while(true)
{
clientSocket=serverSocket.accept();
Connect con=new Connect(clientSocket);
}
}
catch(IOException e)
{
System.out.println("Cannot listen to the client "+e);
}
}
//数据读取开始
class Connect extends Thread
{
ObjectInputStream fromClient;
ObjectOutputStream toClient;
public Connect(Socket netClient)
{
try
{
fromClient=new ObjectInputStream(netClient.getInputStream());
toClient=new ObjectOutputStream(netClient.getOutputStream());
}
catch(Exception e)
{
System.out.println("Cannot get the client stream "+e);
}
this.start();
}
public void run()
{
try
{
while(true)
{
msg=(ChatMessage)fromClient.readObject();
System.out.println("messageType"+" "+msg.messageType);
System.out.println("ChatName"+" "+msg.ChatName);
System.out.println("ToChatName"+" "+msg.ToChatName);
System.out.println("vector"+" "+msg.vector);
System.out.println("vector"+" "+vector);
System.out.println("-------------------------------------");
if(msg.messageType.equals("Login"))
{
/*for(int i=1;i<vector.size();i++)
{
if(vector.get(i).toString().equals(msg.ChatName))
{
ChatMessage msg=new ChatMessage("Error",null,null,null,null);
System.out.println(msg.ChatName);
SentToAll(msg);
break;
}
}*/
sp.TInfo.append(msg.ChatName+" 进入聊天室"+"\n");
vector.addElement(msg.ChatName);
chatname=msg.ChatName;
ChatMessage msgs=new ChatMessage(msg.messageType,msg.ChatName,msg.ToChatName,msg.ChatMsg,vector);
putSocket(msgs.ChatName,toClient);
SentToAll(msgs);
sp.LChater.removeAll();
sp.LChater.setListData(vector);
}
else if(msg.messageType.equals("LoginOut"))
{
sp.TInfo.append(msg.ChatName+" 离开了聊天室"+"\n");
vector.remove(msg.ChatName);
chatname=msg.ChatName;
ChatMessage msgs=new ChatMessage(msg.messageType,msg.ChatName,msg.ToChatName,msg.ChatMsg,vector);
removeSocket(msgs.ChatName);
SentToAll(msgs);
sp.LChater.removeAll();
sp.LChater.setListData(vector);
}
else
{
putSocket(msg.ChatName,toClient);
SentToAll(msg);
}
}
}
catch(Exception e)
{}
}
}
// 数据读取结束
//发送信息开始
void SentToAll(ChatMessage m)
{
try
{
for(Enumeration e=h.elements();e.hasMoreElements();)
{
((ObjectOutputStream)e.nextElement()).writeObject(m);
System.out.println("send messageType"+" "+m.messageType);
System.out.println("send ChatName"+" "+m.ChatName);
System.out.println("send ToChatName"+" "+m.ToChatName);
System.out.println("send vector"+" "+m.vector);
System.out.println("-------------------------------------");
System.out.println("-------------------------------------");
}
}
catch(Exception e)
{}
}
// 发送信息结束
public void actionPerformed(ActionEvent e)
{
Object source=e.getSource();
if(source==sp.button1)//启动服务器
{
try
{
StartServer();
}
catch(Exception ex)
{
System.out.println("Error: "+ex);
}
}
else if(source==sp.button2)//停止服务器
{
StopServer();
}
else if(source==sp.button3)//退出服务器
{
try
{
int returnvalue=JOptionPane.showConfirmDialog(sp.PServer," 确认要关闭服务器吗? ","关闭服务器",JOptionPane.YES_NO_OPTION);
if(returnvalue==JOptionPane.YES_OPTION)
{
System.exit(0);
}
else
{
System.out.println("nothings ");
}
}
catch(Exception ex)
{
System.out.println("Error: "+ex);
}
}
else if(source==sp.button4)//关于
{
JOptionPane.showMessageDialog(sp.PServer," 聊天室版本 V0.3_0524_beat ","关于",JOptionPane.INFORMATION_MESSAGE);
}
}
//运行服务器开始
void StartServer()
{
if(!running)
{
try
{
vector.addElement("所有人");
serverSocket=new ServerSocket(1001);
this.start();
sp.TInfo.append("***服务器启动***"+"\n");
running=true;
sp.button1.setEnabled(false);
sp.button2.setEnabled(true);
}
catch(Exception e)
{
System.out.println("Error: "+e);
}
}
else
{
JOptionPane.showMessageDialog(sp.PServer," 服务器已经打开 ","服务器已经打开",JOptionPane.ERROR_MESSAGE);
System.out.println("服务器运行中");
}
}
// 运行服务器结束
//停止服务器开始
void StopServer()
{
System.out.println(running);
if(running==true)
{
try
{
ChatMessage msgs=new ChatMessage("Closed",null,null,null,null);
if(vector.size()>1)
{
SentToAll(msgs);
vector.clear();
String s="所有人";
vector.removeAllElements();
vector.addElement(s);
running=false;
sp.LChater.removeAll();
sp.LChater.setListData(vector);
}
try
{
//this=null;
System.out.println("Server close");
sp.TInfo.append("***服务器关闭***"+"\n");
serverSocket.close();
}
catch(IOException ioe)
{
System.out.println("Server cant close");
}
running=false;
sp.button1.setEnabled(true);
sp.button2.setEnabled(false);
}
catch(Exception e)
{
System.out.println("Error: "+e);
}
}
else
{
JOptionPane.showMessageDialog(sp.PServer," 服务器已经关闭 ","服务器已经关闭",JOptionPane.ERROR_MESSAGE);
System.out.println("服务器没有运行");
}
}
// 停止服务器结束
public synchronized void putSocket(String name,ObjectOutputStream to)
{
ChatServer.h.put(name,to); notifyAll();
}
public synchronized void removeSocket(String name)
{
try
{
((ObjectOutputStream)h.get(name)).close(); h.remove(name); notifyAll();
}
catch(Exception e)
{
System.out.println("退出用户输出流关闭错误");
}
}
public static void main(String args[])
{
new ChatServer();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -