📄 server.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.io.*;
import java.net.*;
public class Server extends JFrame{
private JButton jb;//用于启动和停止服务的按钮
private Border border;//用于设定按钮边框
private ServerSocket serversocket;//服务套接字
private JComboBox combobox;//显示在线用户列表的组合框
private JTextArea textarea;//显示系统或聊天信息的区编辑框
private JScrollPane js;//区编辑框中的滚动条
private JLabel jlabel;//用于显示在线人数的标签
private JTextField systemmsg;//用于编辑系统信息的行编辑框
private User userinfolist;//用户信息链表
private ServerListenThread listenthread;//服务端侦听线程
public Server()//构造函数
{
super("服务器");//标题栏
init();//初始化函数
setSize(550,430);//设定框架的大小
show();//显示窗口
}
public void init(){//初始化函数
//按钮边框样式
Border bevelBorder=BorderFactory.createBevelBorder(BevelBorder.RAISED,Color.white,Color.white,Color.white,Color.white);
Border emptyBorder=BorderFactory.createEmptyBorder(1,1,1,1);
border=BorderFactory.createCompoundBorder(bevelBorder,emptyBorder);
jb=new JButton();//创建按钮
jb.setText("启动服务");//显示按钮上的字
jb.setForeground(Color.red);//设置按钮字的颜色
jb.addActionListener(new ActionListener()//添加按钮的监听器
{
public void actionPerformed(ActionEvent e)
{
startServer();//启动服务的方法
}
}
);
combobox=new JComboBox();//创建用户列表的组合框
combobox.insertItemAt("全部",0);//添加列表选项
combobox.setSelectedIndex(0);
textarea=new JTextArea(20,20);
textarea.setEditable(false);//设置区编辑框为只读
js=new JScrollPane();//创建滚动条
jlabel=new JLabel();//创建标签
js.getViewport().add(textarea);//在滚动条中添加区编辑框
systemmsg=new JTextField("输入系统信息!~",40);
systemmsg.setEnabled(false);//设置行编辑框为只读
systemmsg.addActionListener(new ActionListener()//为行编辑框添加事件监听器
{
public void actionPerformed(ActionEvent e)
{
sendSystemMessage();//发送系统信息的方法
}
}
);
addWindowListener(new WindowAdapter()//添加主框架的监听器
{
public void windowClosing(WindowEvent e)
{
stopServer();
System.exit(0);
}
}
);
setLayout(null);//布局管理器为空
jb.setBorder(border);
jb.setBounds(10,10,70,30);//设置按钮位置
jlabel.setBounds(90,10,90,20);//设置标签位置
textarea.setBounds(30,55,450,250);//区编辑框位置
js.setBounds(30,55,450,250);//滚动条位置
combobox.setBounds(30,325,90,30);//组合框位置
systemmsg.setBounds(150,325,300,20);//行编辑框位置
getContentPane().add(jb);//将各个组件添加到容器中
getContentPane().add(jlabel);
getContentPane().add(js);
getContentPane().add(combobox);
getContentPane().add(systemmsg);
}
public void startServer()//启动服务方法
{
if(jb.getText()=="启动服务"){//判断按钮上的字是否为"启动服务",如果是,就改成"停止服务",颜色也进行改变
jb.setText("停止服务");
jb.setForeground(Color.blue);
try
{
serversocket=new ServerSocket(8000,10);//设置到端口8000
textarea.append("服务在8000端口\n");
systemmsg.setEnabled(true);//设置为可写
}catch(Exception e)
{
;
}
userinfolist=new User();//创建在线用户信息列表
listenthread=new ServerListenThread(serversocket,combobox,textarea,jlabel,userinfolist);//创建服务端监听线程
listenthread.start();//启动线程
}else{//当按钮上的字为"停止服务"时,进行停止服务的方法
try
{
sendStopToAll();//向所有用户发送关闭服务的方法
listenthread.isstop=true;
serversocket.close();//关闭监听套接字
int count=userinfolist.getCount();//得到在线用户的个数
int i=0;
while(i<count)
{
Dan node=userinfolist.find(i);//根据索引,查找用户
node.input.close();//关闭输入输出流,套接字
node.output.close();
node.socket.close();
i++;
}
systemmsg.setEnabled(false);//设置为可读
combobox.removeAllItems();//删除在线用户列表组合框中的列表项
combobox.addItem("全部");
jb.setText("启动服务");//将按钮字改为启动服务
jb.setForeground(Color.red);
}catch(Exception e)
{;
}
}
}
public void stopServer()//停止服务的方法
{
try
{
sendStopToAll();
listenthread.isstop=true;
serversocket.close();
int count=userinfolist.getCount();
int i=0;
while(i<count)
{
Dan node=userinfolist.find(i);
node.input.close();
node.output.close();
node.socket.close();
i++;
}
systemmsg.setEnabled(false);
combobox.removeAllItems();
combobox.addItem("全部");
}catch(Exception e)
{;
}
}
public void sendStopToAll()//将停止服务的消息发关给各个用户
{
int count=userinfolist.getCount();//得到在线用户的个数
int i=0;
while(i<count)
{
Dan node=userinfolist.find(i);//查找
if(node==null)
{
i++;
continue;
}
try
{
node.output.writeObject("服务关闭");
node.output.flush();
}catch(Exception e)
{;
}
i++;
}
}
public void sendSystemMessage()//实现发送系统消息功能
{
String tosomebody=combobox.getSelectedItem().toString();//得到消息发送对象
String message=systemmsg.getText();//得到消息内容
if(tosomebody.equalsIgnoreCase("全部"))//判断发送对象
{
sendMsgToAll(message);//向所有用户发送消息
}
else//如果发送对象为特定用户,则向该用户发送消息
{
Dan node=userinfolist.find(tosomebody);
try
{
node.output.writeObject("系统信息");//标识
node.output.flush();//发送消息
node.output.writeObject(message);
node.output.flush();//发送消息
}
catch(Exception e)
{;
}
}
}
public void sendMsgToAll(String msg)//向所有在线用户发送系统消息
{
int count=userinfolist.getCount();//得到用户个数
int i=0;
while(i<count)
{
Dan node=userinfolist.find(i);
if(node==null)
{
i++;
continue;
}
try
{
node.output.writeObject("系统信息");
node.output.flush();
node.output.writeObject(msg);
node.output.flush();
}
catch(Exception e)
{;
}
i++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -