实现多线程聊天室,在Java中可以通过使用Socket和Thread来实现。
具体步骤如下:
1.创建服务器端
- 创建ServerSocket对象,并设置端口号
- 创建Socket对象,以接受客户端请求
- 使用Thread创建一个线程,以接受客户端发来的消息,并将消息广播给其他客户端
- 使用ArrayList存储客户端(每个客户端都对应一个Socket对象)
- 使用while循环,以接受客户端请求并处理
示例1:
下面是创建线程的示例代码。
public class ChatServer {
private ArrayList clientList = new ArrayList();
private ServerSocket serverSocket;
public void start() {
try {
serverSocket = new ServerSocket(1024);
System.out.println("server start...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("accept client:" + socket.getInetAddress());
ChatHandler handler = new ChatHandler(socket, clientList);
new Thread(handler).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class ChatHandler implements Runnable {
private Socket socket;
private BufferedReader reader;
private PrintWriter writer;
private ArrayList clientList;
public ChatHandler(Socket socket, ArrayList clientList) {
this.socket = socket;
this.clientList = clientList;
try {
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
String line;
while ((line = reader.readLine()) != null) {
broadcast(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void broadcast(String msg) {
for (int i = 0; i < clientList.size(); i++) {
Socket s = (Socket) clientList.get(i);
if (s != this.socket) {
try {
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
示例2:
下面是客户端实现的示例代码。
public class ChatClient extends JFrame {
private JTextArea msgarea = new JTextArea();
private JTextField msginput = new JTextField();
private JButton btn = new JButton("发送");
private PrintWriter pw;
public ChatClient() {
init();
connect();
}
private void init() {
this.setSize(400, 400);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
JScrollPane sp = new JScrollPane(msgarea);
msgarea.setEditable(false);
this.add(sp, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BorderLayout());
bottomPanel.add(msginput, BorderLayout.CENTER);
bottomPanel.add(btn, BorderLayout.EAST);
this.add(bottomPanel, BorderLayout.SOUTH);
btn.addActionListener(e -> {
String msg = msginput.getText();
pw.println(msg);
msginput.setText("");
});
}
private void connect() {
try {
Socket socket = new Socket("localhost", 1024);
pw = new PrintWriter(socket.getOutputStream(), true);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
new Thread(() -> {
String line;
try {
while ((line = br.readLine()) != null) {
msgarea.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new ChatClient().setVisible(true);
}
}
至此,Java实现多线程聊天室的完整攻略就讲解完了。如果你想了解更多关于Java多线程编程,可以参考Java的官方文档或者相关教程。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java实现多线程聊天室 - Python技术站