利用java socket 服务端客户端采用多线程,主线程负责接收请求,由主线程启动5个工作线程,每个工作线程隔100-1000毫秒,从11个地区号中随机抽取一个,开始5001端口,显示返回地区名称,每个线程抽取10次,即客户端共发起50个请求
时间: 2023-11-21 20:03:53 浏览: 97
好的,这个问题其实是对之前提到的多线程和Socket的应用。下面是利用Java Socket实现服务端和客户端多线程之间的交互,实现上述要求的代码:
服务端代码:
```java
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(5001);
System.out.println("Server started");
for (int i = 1; i <= 5; i++) {
WorkerThread workerThread = new WorkerThread(i);
workerThread.start();
}
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
ClientHandler clientHandler = new ClientHandler(socket);
clientHandler.start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
上述代码中,我们首先创建了一个ServerSocket对象,并且在端口5001上启动服务。然后我们启动了5个工作线程WorkerThread,并且每个线程随机从11个地区中抽取一个,并且显示返回地区名称。
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Random;
public class ClientHandler extends Thread {
final Socket socket;
public ClientHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
String request = input.readLine();
System.out.println("Received request: " + request);
Random random = new Random();
String[] areas = {"Beijing", "Shanghai", "Guangzhou", "Shenzhen", "Hangzhou", "Chengdu", "Chongqing", "Wuhan", "Nanjing", "Suzhou", "Tianjin"};
for (int i = 1; i <= 10; i++) {
int areaIndex = random.nextInt(11);
int port = 5001 + i;
String response = areas[areaIndex];
Thread.sleep(random.nextInt(901) + 100);
output.println(response);
System.out.println("Sent response: " + response);
}
socket.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}
```
上述代码中,我们创建了一个ClientHandler类,继承自Thread类,并且重写了run()方法。在run()方法中,我们首先从客户端读取请求,然后随机从11个地区中抽取一个,并且显示返回地区名称。在发送每个响应之间,我们让线程随机休眠100到1000毫秒,模拟真实场景下的网络延迟。
客户端代码:
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class Client {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 5001);
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
for (int i = 1; i <= 50; i++) {
String request = "Request " + i;
output.println(request);
System.out.println("Sent request: " + request);
String response = input.readLine();
System.out.println("Received response: " + response);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
上述代码中,我们创建了一个Socket对象,并且连接到了服务端的IP地址和端口号。然后我们向服务端发送50个请求,并且输出发送和接收的请求和响应。
注意:上述代码仅为示例代码,可能无法直接运行,需要根据具体情况进行调整和修改。
阅读全文