java stocket 实现服务器于客户机的交互,发送数据,创建新的客户机,客户机与服务器的连接,广播的代码与注释
时间: 2024-02-12 15:09:10 浏览: 71
socket编程 实现服务器和客户的通信
5星 · 资源好评率100%
好的,以下是一个简单的Java Socket实现服务器与客户端之间交互、发送数据、创建新客户机、客户端与服务器连接和广播的示例代码及注释:
服务器端代码:
```java
import java.io.*;
import java.net.*;
public class Server {
private ServerSocket serverSocket;
public static void main(String[] args) {
new Server().startServer();
}
public void startServer() {
try {
// 创建ServerSocket并绑定端口号
serverSocket = new ServerSocket(8888);
System.out.println("服务器已启动,等待客户端连接...");
while (true) {
// 接收客户端连接请求,创建新的客户端Socket
Socket clientSocket = serverSocket.accept();
System.out.println("有新的客户端连接:" + clientSocket.getInetAddress().getHostAddress());
// 创建新线程处理客户端请求
ClientHandler clientHandler = new ClientHandler(clientSocket);
new Thread(clientHandler).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 处理客户端请求的线程
private class ClientHandler implements Runnable {
private Socket clientSocket;
private BufferedReader reader;
private PrintWriter writer;
public ClientHandler(Socket clientSocket) throws IOException {
this.clientSocket = clientSocket;
// 获取输入流和输出流
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
}
@Override
public void run() {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println("收到客户端消息:" + message);
// 广播消息给其他客户端
broadcastMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 发送消息给客户端
public void sendMessage(String message) {
writer.println(message);
writer.flush();
}
}
// 广播消息给所有客户端
public synchronized void broadcastMessage(String message) {
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread instanceof ClientHandler) {
ClientHandler clientHandler = (ClientHandler) thread;
clientHandler.sendMessage(message);
}
}
}
}
```
客户端代码:
```java
import java.io.*;
import java.net.*;
public class Client {
private Socket clientSocket;
private BufferedReader reader;
private PrintWriter writer;
public static void main(String[] args) {
new Client().startClient();
}
public void startClient() {
try {
// 创建客户端Socket并连接服务器
clientSocket = new Socket("localhost", 8888);
System.out.println("连接服务器成功!");
// 获取输入流和输出流
reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
writer = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
// 开启新线程接收服务器消息
new Thread(() -> {
try {
String message;
while ((message = reader.readLine()) != null) {
System.out.println("收到服务器消息:" + message);
}
} catch (IOException e) {
e.printStackTrace();
}
}).start();
// 发送消息给服务器
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String message;
while ((message = consoleReader.readLine()) != null) {
writer.println(message);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
注释在代码中,希望对您有所帮助。
阅读全文