写代码利用Java的Socket通信,实现一款即时通信软件,能够允许任意多的客户端的参与,客户端需要图形界面。
时间: 2024-02-17 22:04:07 浏览: 61
好的,以下是Java Socket通信实现即时通信软件的代码示例:
服务器端代码:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
// 定义服务器端口号
public static final int PORT = 8888;
// 定义客户端列表
private List<ClientHandler> clients = new ArrayList<>();
public void start() {
try {
// 创建服务器Socket
ServerSocket serverSocket = new ServerSocket(PORT);
System.out.println("服务器已启动,等待客户端连接...");
// 循环等待客户端连接
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
// 创建客户端处理线程
ClientHandler handler = new ClientHandler(socket);
// 添加客户端到列表
clients.add(handler);
// 启动客户端处理线程
handler.start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 广播消息给所有客户端
public synchronized void broadcast(String message) {
for (ClientHandler handler : clients) {
handler.send(message);
}
}
// 客户端处理线程类
class ClientHandler extends Thread {
private Socket socket;
private BufferedReader reader;
private BufferedWriter writer;
public ClientHandler(Socket socket) {
this.socket = socket;
try {
// 创建输入输出流
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
// 发送消息给客户端
public void send(String message) {
try {
writer.write(message + "\n");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
// 接收客户端消息
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) {
// 客户端断开连接
System.out.println("客户端已断开:" + socket.getInetAddress().getHostAddress());
// 从列表中移除客户端
clients.remove(this);
// 关闭输入输出流和Socket
reader.close();
writer.close();
socket.close();
break;
} else {
// 广播消息给所有客户端
broadcast(message);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Server().start();
}
}
```
客户端代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client extends JFrame {
// 定义服务器IP地址和端口号
public static final String SERVER_HOST = "localhost";
public static final int SERVER_PORT = 8888;
// 定义客户端Socket
private Socket socket;
// 定义输入输出流
private BufferedReader reader;
private BufferedWriter writer;
// 定义GUI组件
private JTextArea messageArea;
private JTextField inputField;
public Client() {
// 创建GUI组件
messageArea = new JTextArea();
messageArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(messageArea);
inputField = new JTextField();
JButton sendButton = new JButton("发送");
// 添加事件监听器
sendButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
// 发送消息给服务器
try {
writer.write(message + "\n");
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
inputField.setText("");
}
}
});
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sendButton.doClick();
}
});
// 添加GUI组件到窗口
JPanel panel = new JPanel(new BorderLayout());
panel.add(scrollPane, BorderLayout.CENTER);
panel.add(inputField, BorderLayout.SOUTH);
panel.add(sendButton, BorderLayout.EAST);
setContentPane(panel);
setTitle("即时通信软件");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 300);
setLocationRelativeTo(null);
setVisible(true);
// 连接服务器
try {
socket = new Socket(SERVER_HOST, SERVER_PORT);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 启动消息接收线程
new MessageReceiver().start();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "无法连接到服务器", "错误", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
// 消息接收线程类
class MessageReceiver extends Thread {
public void run() {
try {
while (true) {
String message = reader.readLine();
if (message == null) {
// 服务器断开连接
JOptionPane.showMessageDialog(Client.this, "服务器已断开连接", "错误", JOptionPane.ERROR_MESSAGE);
System.exit(0);
} else {
// 收到消息更新GUI组件
messageArea.append(message + "\n");
messageArea.setCaretPosition(messageArea.getDocument().getLength());
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Client();
}
}
```
以上是基于Java Socket通信实现即时通信软件的代码示例,希望能对您有所帮助。
阅读全文