写代码Java实现socket即时通信软件,客户端需要显示端口,IP和昵称且昵称不可以与其他聊天室重复
时间: 2024-02-17 19:04:29 浏览: 59
基于Java实现socket通信源代码
好的,让我们来具体实现这个即时通信软件。首先,我们需要实现服务器端和客户端的代码。
服务器端代码:
```java
import java.io.*;
import java.net.*;
import java.util.*;
public class ChatServer {
// 存储所有已连接的客户端昵称
private static List<String> nicknames = new ArrayList<String>();
// 存储所有已连接的客户端的输出流
private static List<PrintWriter> writers = new ArrayList<PrintWriter>();
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
try {
while (true) {
Socket socket = serverSocket.accept();
System.out.println("客户端 " + socket.getRemoteSocketAddress() + " 连接成功!");
new HandlerThread(socket).start();
}
} finally {
serverSocket.close();
}
}
// 处理客户端请求的线程
private static class HandlerThread extends Thread {
private Socket socket;
private String nickname;
private BufferedReader in;
private PrintWriter out;
public HandlerThread(Socket socket) {
this.socket = socket;
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 获取客户端昵称,并检查是否已经被占用
while (true) {
out.println("Please enter your nickname:");
nickname = in.readLine();
if (nickname == null) {
return;
}
synchronized (nicknames) {
if (!nicknames.contains(nickname)) {
nicknames.add(nickname);
break;
}
}
}
// 显示客户端信息
out.println("Welcome " + nickname + " to the chat room!");
out.println("Your IP address is " + socket.getInetAddress().getHostAddress());
out.println("Your port number is " + socket.getPort());
// 将客户端输出流加入到输出流列表中
writers.add(out);
// 广播新客户端加入的信息
for (PrintWriter writer : writers) {
writer.println(nickname + " has joined the chat room!");
}
// 处理客户端请求
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println(nickname + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
// 客户端断开连接,从输出流列表中移除
if (nickname != null) {
nicknames.remove(nickname);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
```
客户端代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ChatClient {
private JFrame frame;
private JTextField textField;
private JTextArea textArea;
private String nickname;
private BufferedReader in;
private PrintWriter out;
public static void main(String[] args) throws Exception {
ChatClient client = new ChatClient();
client.run();
}
public void run() throws Exception {
frame = new JFrame("Chat Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
textArea = new JTextArea(15, 50);
textArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
textField = new JTextField(50);
textField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
out.println(textField.getText());
textField.setText("");
}
});
panel.add(textField, BorderLayout.SOUTH);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
String serverAddress = JOptionPane.showInputDialog(frame, "Enter IP address of the server:");
Socket socket = new Socket(serverAddress, 8888);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 获取客户端昵称,并检查是否已经被占用
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
if (input.startsWith("Please enter your nickname:")) {
nickname = JOptionPane.showInputDialog(frame, input);
if (nickname == null) {
return;
}
out.println(nickname);
} else if (input.startsWith("Welcome ")) {
textArea.append(input + "\n");
} else if (input.startsWith("Your IP address is ")) {
textArea.append(input + "\n");
} else if (input.startsWith("Your port number is ")) {
textArea.append(input + "\n");
break;
}
}
// 处理服务器广播的信息
while (true) {
String input = in.readLine();
if (input == null) {
return;
}
textArea.append(input + "\n");
}
}
}
```
在这个代码中,我们实现了服务器端和客户端的代码。服务器端可以支持多个客户端连接,并且存储了所有已连接的客户端昵称和输出流。当新客户端连接到服务器时,服务器会检查该客户端昵称是否已经被占用,并将该客户端的输出流加入到输出流列表中。客户端可以显示自己的昵称、IP地址和端口号,并且支持向服务器发送消息。当客户端发送消息时,服务器会将该消息广播给所有已连接的客户端。
在这个代码中,我们实现了昵称不可以与其他聊天室重复的要求。当新客户端连接到服务器时,服务器会检查该客户端昵称是否已经被占用。如果该昵称已经被占用,服务器会要求客户端重新输入昵称。
阅读全文