(1) 将课程中的tcpthread.SimpleTCPClient修改为多线程方式,模拟多个客户端同时向服务的发起服务情况; (2) 客户端main方法中,根据String[] msgs = {“Hello ”," Whut", "Info ", "2021 ","Java "}; 字符串长度创建5个线程,第i个线程完成(a)连接服务器端;(b)主动向服务器发送第i个字符串;(c)接收服务器的返回,并输出到控制台 (3) 服务器仍采用多线程方式。main线程用于等待客户端请求,接收到客户端请求后,创建一个新线程处理该客户端请求; (4) 服务器线程完成的工作主要有:(a)接收客户端线程发送的消息msg,并输出到控制台;(b)将按 “Echo from server: ” + msg 方式拼接后的数据发送给客户端;(c)关闭端口
时间: 2023-06-25 13:04:21 浏览: 90
以下是修改后的代码:
SimpleTCPClient.java:
```
import java.io.*;
import java.net.*;
public class SimpleTCPClient extends Thread {
private String message;
private int port;
public SimpleTCPClient(int port, String message) {
this.port = port;
this.message = message;
}
public void run() {
try {
Socket clientSocket = new Socket("localhost", port);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.println(message);
System.out.println("Sent: " + message);
String response = in.readLine();
System.out.println("Received: " + response);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
ClientMain.java:
```
public class ClientMain {
public static void main(String[] args) {
String[] msgs = {"Hello ", " Whut", "Info ", "2021 ", "Java "};
for (int i = 0; i < msgs.length; i++) {
String message = msgs[i];
new SimpleTCPClient(12345, message).start();
}
}
}
```
SimpleTCPServer.java:
```
import java.io.*;
import java.net.*;
public class SimpleTCPServer extends Thread {
private Socket clientSocket;
public SimpleTCPServer(Socket clientSocket) {
this.clientSocket = clientSocket;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
String message = in.readLine();
System.out.println("Received: " + message);
String response = "Echo from server: " + message;
out.println(response);
System.out.println("Sent: " + response);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(12345);
while (true) {
Socket clientSocket = serverSocket.accept();
new SimpleTCPServer(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)