📄 serverframe.java
字号:
//package ZHWJBChat;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
class ServerFrame extends JFrame
{
JPanel contentPane; // 定义图形界面变量
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenuFile = new JMenu();
JMenuItem jMenuFileExit = new JMenuItem();
JMenu jMenuHelp = new JMenu();
JMenuItem jMenuHelpAbout = new JMenuItem();
static JLabel statusBar = new JLabel();
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout2 = new BorderLayout();
JLabel jLabel1 = new JLabel();
static java.awt.List jList1 = new java.awt.List(13);
JScrollPane scrollpane = new JScrollPane(jList1);
static Vector clients = new Vector(10); // 用vector向量数组存储连接客户变量
static ServerSocket server = null; // 建立服务器socket
static int active_connects = 0; // 用来存储目前连接的客户数
static Socket socket = null;
public static void main(String[] args)
{
ServerFrame ServerFrame1=new ServerFrame(); // 实例化一个ServerFrame类
ServerFrame1.show();
System.out.println("Server starting ...");
try
{
server=new ServerSocket(8080); // 使用端口8080初始化服务器套接字
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
while(true)
{
try
{
socket = server.accept(); // 用来存储连接上的客户socket
if(socket != null)
{
System.out.println(socket + "连接"); // 在服务器控制台打印客户连接信息
}
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
ClientEchoThread c = new ClientEchoThread(socket); // 定义并实例化一个ClientEchoThread线程类,对应一个客户连接
clients.addElement(c); // 加入clients数组中
if(checkName(c))
{ // 调用checkName方法验证客户的合法性
int connum = ++ServerFrame1.active_connects; // 定义connum来存储活动连接数
String constr = "目前有" + connum + "客户相连"; // 在状态栏里显示连接数
ServerFrame1.statusBar.setText(constr);
ServerFrame1.jList1.addItem(c.IP + "连接"); // 将客户socket信息写入list框
c.start();
String s = "SYSTEM:" + c.name + "加入聊天室!";
sendClients(s); // 启动线程
notifyRoom(); // 用notifyRoom方法来监视聊天室连接变化
}
else
{
c.send("QUIT");
disconnect(c);
}
}
}
public ServerFrame()
{
try
{
jbInit();
}catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("聊天服务器端");
statusBar.setText("目前的连接数为:");
jMenuFile.setText("File");
jMenuFileExit.setText("Exit");
jMenuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuFileExit_actionPerformed(e);
}
});
jMenuHelp.setText("Help");
jMenuHelpAbout.setText("About");
jMenuHelpAbout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
jMenuHelpAbout_actionPerformed(e);
}
});
jPanel1.setLayout(borderLayout2);
jLabel1.setText("服务器端连接客户");
jMenuFile.add(jMenuFileExit);
jMenuHelp.add(jMenuHelpAbout);
jMenuBar1.add(jMenuFile);
jMenuBar1.add(jMenuHelp);
this.setJMenuBar(jMenuBar1);
contentPane.add(statusBar, BorderLayout.SOUTH);
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jLabel1, BorderLayout.NORTH);
jPanel1.add(scrollpane, BorderLayout.CENTER);
} // end of jbinit
public void jMenuFileExit_actionPerformed(ActionEvent e)
{ // 退出菜单方法
sendClients("QUIT"); // 向客户端发送断开连接信息
closeAll(); // 调用closeAll方法关闭所有连接
System.exit(0);
}
public void jMenuHelpAbout_actionPerformed(ActionEvent e)
{
chatServer_AboutBox dlg = new chatServer_AboutBox(this);
Dimension dlgSize = dlg.getPreferredSize();
Dimension frmSize = getSize();
Point loc = getLocation();
dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
dlg.setModal(true);
dlg.show();
}
protected void processWindowEvent(WindowEvent e)
{ // 关闭服务器程序要进行的操作
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING)
{
jMenuFileExit_actionPerformed(null);
}
}
public static void notifyRoom()
{
String people = "PEOPLE";
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
people += ":" + c.name + '|' + c.IP;
}
sendClients(people);
}
public static synchronized void sendClients(String msg)
{ // 向每个连接的客户端发送信息
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
c.send(msg);
}
}
public static void closeAll()
{ // 关闭所有连接信息
while(clients.size() > 0)
{ // 遍历clients数组删除所有连接客户信息
ClientEchoThread c = (ClientEchoThread)clients.firstElement();
try
{
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
finally
{
clients.removeElement(c);
}
}
} //closeAll方法结束
public static boolean checkName(ClientEchoThread newclient)
{ // 检查连接客户的socket信息是否合法
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if((c != newclient) && c.equals(newclient.name)) return false;
}
return true;
} // end of checkName method
public static synchronized void disconnect(ClientEchoThread c)
{ // 断开单个客户方法
try
{
jList1.addItem(c.IP + "断开连接"); // 在服务器端程序的list框中显示断开信息
int connum = --ServerFrame.active_connects; // 连接数减1
String constr = "目前有" + connum + "客户相连"; // 刷新状态栏里的连接数
statusBar.setText(constr);
c.send("QUIT"); // 向客户发送断开连接信息
//sendClients("SYSTEM:" + c.name + "离开聊天室!");
c.socket.close();
}
catch(IOException e)
{
System.out.println("Error:" + e);
}
finally
{
clients.removeElement(c); // 从clients数组中删除客户的相关socket信息
}
}
static ClientEchoThread findFromName(String name)
{
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if(name.equals(c.name)) return c;
}
return null;
}
static String findIPFromName(String name)
{
for(int i = 0; i < clients.size(); i++)
{
ClientEchoThread c = (ClientEchoThread)clients.elementAt(i);
if(name.equals(c.name)) return c.IP;
}
return null;
}
}
class ClientEchoThread extends Thread
{ // 定义ClientEchoThread线程类
Socket socket; // 存储一个连接客户的socket信息
String name; // 存储客户的连接姓名
String IP;
String netMeetingFlag = "refuse"; // 存储客户的IP信息
BufferedReader dis; // 接收从客户端发来的数据流
PrintWriter ps; // 向客户端发送信息的打印流
public void send(String msg)
{ // 向客户端发送信息的方法
ps.println(msg); // 打印流发送信息
ps.flush();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -