代码实现

public class SocketClient {
public static void main(String[] args) throws IOException {
Socket so = new Socket("175.0.0.1",8888);
OutputStream ops = so.getOutputStream();
ops.write("你好".getBytes());
InputStream ips = so.getInputStream();
byte[] bytes = new byte[1024];
int len = ips.read(bytes);
System.out.println(new String(bytes,0,len));
so.close();
}
}
public class SocketServer {
public static void main(String[] args) throws IOException {
ServerSocket sso = new ServerSocket(8888);
Socket socket = sso.accept();
InputStream ips = socket.getInputStream();
byte[] bytes = new byte[1024];
int len = ips.read(bytes);
System.out.println(new String(bytes,0,len));
OutputStream ops = socket.getOutputStream();
ops.write("收到了".getBytes());
socket.close();
sso.close();
}
}
害,代码跟视频的差不多,但是就是运行不出结果
本文提供了一个简单的Socket通信示例,包括客户端和服务端的Java代码实现,演示了如何发送和接收消息。

511

被折叠的 条评论
为什么被折叠?



