JAVA 聊天室服务器端和客户端完整代码
发布网友
发布时间:2022-04-28 21:30
我来回答
共1个回答
热心网友
时间:2022-04-20 02:18
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.util.*;
import java.io.*;
public class Server extends JFrame
{
JMenuBar mb = new JMenuBar();
JMenu m1 = new JMenu("文件");
JMenu m2 = new JMenu("帮助");
JMenuItem mi1 = new JMenuItem("连接");
JMenuItem mi2 = new JMenuItem("退出");
JLabel lab1 = new JLabel("用户在线列表");
JLabel lab2 = new JLabel("消息:");
JTextArea ta = new JTextArea(9,30);
java.awt.List list = new java.awt.List(9);
JTextField tf = new JTextField(24);
JButton b = new JButton("发送");
JScrollPane sp = new JScrollPane();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JPanel p3 = new JPanel();
ServerSocket serverSocket;
Socket socket;
ArrayList array = new ArrayList();
int connect;
public Server()
{
super("聊天服务器");
setJMenuBar(mb);
mb.add(m1);
mb.add(m2);
m1.add(mi1);
m1.addSeparator();
m1.add(mi2);
sp.getViewport().add(ta);
p1.setLayout(new BorderLayout());
p3.add(lab1);
p1.add(p3,BorderLayout.NORTH);
p1.add(list,BorderLayout.SOUTH);
p2.add(lab2);
p2.add(tf);
p2.add(b);
getContentPane().add(sp,BorderLayout.NORTH);
getContentPane().add(p1,BorderLayout.CENTER);
getContentPane().add(p2,BorderLayout.SOUTH);
pack();
//服务器端界面居中显示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = this.getSize();
if(frameSize.width > screenSize.width)
{
frameSize.width = screenSize.width;
}
if(frameSize.height > screenSize.height)
{
frameSize.height = screenSize.height;
}
setLocation((screenSize.width - frameSize.width)/2,
(screenSize.height - frameSize.height)/2);
setSize(400,430);
setVisible(true);
setResizable(false);
mi1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(serverSocket == null)
{
try
{
serverSocket = new ServerSocket(7000);
ta.append("系统提示:"+" 聊天服务器"
+" 正在启动......\n");
}
catch(IOException ex)
{
ta.append("系统提示:"+" 聊天服务器"
+" 启动失败!\n");
}
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经启动......\n");
}
//启动监听客户端连接线程
Thread t = new Watch();
t.start();
}
});
mi2.addActionListener(new ServerListener());
b.addActionListener(new ServerListener());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
closeAll();
System.exit(0);
}
});
}
class Watch extends Thread
{
public void run()
{
while(true)
{
if(array.size() <= 10)
{
try
{
socket = serverSocket.accept();
}
catch(IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
//创建客户端线程,一个线程对应一个客户端
Client c = new Client(socket);
array.add(c);
c.start();
}
else
{
ta.append("系统消息:"+" 聊天服务器"
+" 已经超过最大连接数,请待会连接!\n");
}
}
}
}
class Client extends Thread
{
String name;
String ip;
String ipAddress;
Socket socket;
BufferedReader br;
PrintStream ps;
public Client(Socket s)
{
try
{
socket = s;
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
}
catch (IOException ex)
{
ta.append("系统消息: "+ex+"\n");
}
}
public void run()
{
String info;
try
{
while(true)
{
info = br.readLine();
if(info == null)
break;
StringTokenizer st = new StringTokenizer(info, ":");
String keyword = st.nextToken();
//处理客户端传来的信息
if (keyword.equals("CONNECT"))
{
name = st.nextToken();
ip = st.nextToken();
ipAddress = ip.substring(ip.indexOf("/")+1);
ta.append("系统消息: " + name + "/"
+ ipAddress +" 已经连接\n");
list.add(name + " " + ip
+" 已经登录");
notifyChatRoom();
}
else if (keyword.equals("MSG"))
{
String broadcast = st.nextToken();
StringBuffer message = new StringBuffer("MSG:" +broadcast);
message.append(st.nextToken("\0"));
if (broadcast.equals("BROAD"))
{