请编写程序实现基于TCP/IP协议的Socket通信过程。 服务器端程序平台已经编写完毕。需要你根据提示,在右侧编辑器补充客户端代码。 服务器端开放: IP地址:127.0.0.1 端口号:8889 客户端发送数据: Hello Server! 服务器端代码: public class Server { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8889); System.out.println("启动服务器...."); Socket s = ss.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); String mess = br.readLine(); System.out.println("客户端:" + mess); ss.close(); } catch (IOException e) { e.printStackTrace(); } } } 要求输出的结果为: 启动服务器... 客户端:测试客户端与服务器通信:Hello Server!
时间: 2023-12-20 19:06:49 浏览: 88
```java
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) {
try {
Socket s = new Socket("127.0.0.1", 8889);
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.write("Hello Server!");
pw.flush();
s.shutdownOutput();
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String mess = br.readLine();
System.out.println("服务器:" + mess);
br.close();
pw.close();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文