使用Java语言,利用多线程、网络编程设计一个聊天程序。要求通过监听指定的端口号、目标IP地址和目标端口号,实现消息的发送和接收功能,并把聊天内容显示出来。
时间: 2024-02-05 17:11:33 浏览: 69
好的,以下是一个简单的聊天程序设计思路:
1. 创建一个客户端和一个服务端,客户端用于发送消息,服务端用于接收消息并转发给其他客户端。
2. 客户端与服务端之间通过网络编程通信,使用Socket类连接服务端。
3. 客户端和服务端都需要实现多线程,以便能够同时处理多个消息。
4. 客户端通过读取用户输入,将消息发送给服务端,服务端将收到的消息广播给其他客户端。
5. 在客户端和服务端中都需要实现界面,用于显示聊天内容和输入框。
以下是示例代码:
服务端:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Server {
private ArrayList<ClientHandler> clients = new ArrayList<>();
private ServerSocket serverSocket;
public void start(int port) throws IOException {
serverSocket = new ServerSocket(port);
System.out.println("Server started on port " + port);
while (true) {
Socket clientSocket = serverSocket.accept();
ClientHandler client = new ClientHandler(clientSocket, this);
clients.add(client);
client.start();
System.out.println("New client connected: " + clientSocket.getInetAddress());
}
}
public void broadcast(String message, ClientHandler sender) {
for (ClientHandler client : clients) {
if (client != sender) {
client.sendMessage(sender.getClientName() + ": " + message);
}
}
}
public void removeClient(ClientHandler client) {
clients.remove(client);
}
public static void main(String[] args) {
Server server = new Server();
try {
server.start(12345);
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
客户端:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
private String serverAddress;
private int serverPort;
private String clientName;
private BufferedReader in;
private PrintWriter out;
private JFrame frame = new JFrame("Chat");
private JTextField textField = new JTextField(40);
private JTextArea messageArea = new JTextArea(8, 40);
public Client(String serverAddress, int serverPort, String clientName) {
this.serverAddress = serverAddress;
this.serverPort = serverPort;
this.clientName = clientName;
textField.setEditable(true);
messageArea.setEditable(false);
frame.getContentPane().add(textField, BorderLayout.SOUTH);
frame.getContentPane().add(new JScrollPane(messageArea), BorderLayout.CENTER);
frame.pack();
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
}
public void start() {
try {
Socket socket = new Socket(serverAddress, serverPort);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
out.println(clientName);
while (true) {
String line = in.readLine();
if (line.startsWith("SUBMITNAME")) {
out.println(clientName);
} else if (line.startsWith("NAMEACCEPTED")) {
textField.setEditable(true);
} else if (line.startsWith("MESSAGE")) {
messageArea.append(line.substring(8) + "\n");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String serverAddress = "localhost";
int serverPort = 12345;
String clientName = JOptionPane.showInputDialog(null, "Enter your name:", "Name selection", JOptionPane.PLAIN_MESSAGE);
Client client = new Client(serverAddress, serverPort, clientName);
client.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
client.frame.setVisible(true);
client.start();
}
}
```
以上代码仅供参考,不一定完全正确,也不一定满足所有需求。需要根据具体情况进行修改和完善。
阅读全文