⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverframe.java

📁 多人聊天室源代码 一个服务器 多人进入
💻 JAVA
字号:
//ServerFrame 类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import java.net.*;

public class ServerFrame extends JFrame
{
	private JButton jbStartServer;	//开始按钮
	private JButton jbStopServer;	//停止按钮
	private ServerSocket serverSocket;	//服务器Serversocket对象
	private JComboBox combobox;	//用户列表
	private JTextArea textarea;	//聊天室信息
	private JTextField textfield;	//登陆状态信息
	private JTextField systemMsg;	//服务器发送系统信息框
	private UserInfoList userInfoList;	//用户信息
	private ServerListenThread listenThread;	//服务器监听线程
	
	public ServerFrame()
	{
		super("ServerFrame");
		jbStartServer = new JButton("开始服务");
		jbStartServer.setEnabled(true);
		jbStopServer = new JButton("停止服务");
		jbStopServer.setEnabled(false);
		combobox = new JComboBox();
		combobox.insertItemAt("所有人",0);
		combobox.setSelectedIndex(0);
		textarea = new JTextArea(20,20);
		textarea.setEditable(false);
		textfield = new JTextField(40);
		textfield.setEditable(false);
		systemMsg = new JTextField(40);
		systemMsg.setEnabled(false);
		Container c = getContentPane();
		c.setLayout(null);
		jbStartServer.setBounds(10,30,110,30);
		jbStopServer.setBounds(160,30,110,30);
		textfield.setBounds(10,75,300,20);
		textarea.setBounds(10,110,300,300);
		combobox.setBounds(10,470,300,20);
		systemMsg.setBounds(10,470,300,20);
		c.add(jbStartServer,null);
		c.add(jbStopServer,null);
		c.add(textfield,null);
		JScrollPane JP = new JScrollPane(textarea);
		JP.setBounds(10,110,300,300);
		c.add(JP);
		c.add(systemMsg,null);
		setSize(330,550);
		setVisible(true);
		jbStartServer.addActionListener(new ActionListener()
		{	public void actionPerformed(ActionEvent e)
			{startServer();}
		});
		jbStopServer.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{stopServer();}
		});
		systemMsg.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{sendSystemMessage();}
		});
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{System.exit(0);}
		});
	}
	public static void main(String[] args)
	{
		new ServerFrame();
	}
	public void startServer()
	{
		try
		{
			serverSocket = new ServerSocket(8000,10);
			textarea.append("server start at 8000 port...\n");
			jbStartServer.setEnabled(false);
			jbStopServer.setEnabled(true);
			systemMsg.setEnabled(true);
		}
		catch(Exception e){}
		userInfoList = new UserInfoList();
		listenThread = new ServerListenThread(serverSocket,combobox,textarea,textfield,userInfoList);
		listenThread.start();
	}
	public void stopServer()
	{
		try
		{
			sendStopToAll();
			listenThread.isStop = true;
			serverSocket.close();
			int count = userInfoList.getCount();
			int i = 0;
			while(i < count)
			{
				Node node = userInfoList.find(i);
				node.input.close();
				node.output.close();
				node.socket.close();
				i++;
			}
			jbStartServer.setEnabled(true);
			jbStopServer.setEnabled(false);
			systemMsg.setEnabled(false);
			combobox.removeAllItems();
			combobox.addItem("所有人");
		}
		catch(Exception e){}
	}
	public void sendStopToAll()
	{
		int count = userInfoList.getCount();
		int i = 0;
		while(i < count)
		{
			Node node = userInfoList.find(i);
			if(node == null)
			{
				i++;
				continue;
			}
			try
			{
				node.output.writeObject("服务关闭");
				node.output.flush();
			}
			catch(Exception e){}
			i++;
		}
	}
	public void sendMsgToAll(String msg)
	{
		int count = userInfoList.getCount();
		int i = 0;
		while(i < count)
		{
			Node 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++;
		}
	}
	public void sendSystemMessage()
	{
		String toSomebody = combobox.getSelectedItem().toString();
		String message = systemMsg.getText() + "\n";
		textarea.append(message);
		if(toSomebody.equalsIgnoreCase("所有人"))
			sendMsgToAll(message);
		else
		{
			Node node = userInfoList.find(toSomebody);
			try
			{
				node.output.writeObject("系统信息");
				node.output.flush();
				node.output.writeObject(message);
				node.output.flush();
			}
			catch(Exception e){}
		}
	}
}

















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -