ServerSocket server = new ServerSocket(9999); //创建客户端,端口为9999
Socket socket = server.accept(); //客户端与服务端连接
InputStream in = socket.getInputStream();
BufferedReader br = new BufferedReader (new InputStreamReader(in));//将字节流转化为字符流,用缓冲流接收
String line = br.readLine();
System.out.println(("服务端接收到的客户端请求" + line));
OutputStream out = new OutputStream();
out.write("".getBytes());
socket.shutdownOutput(); //关闭输出流
socket.shutdownInput();//关闭输入流
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); //创建客户端,IP地址为本机地址,连接端口为9999的服务端。
OutputStream out = socket.getOutputStream();
out.write();
InputStream in = socket.getInputStream(); //接收服务端响应的内容。
BufferedReader br = new BufferedReader (new InputStreamReader(in));//将字节流转化为字符流,用缓冲流接收。
String line = br.readLine();
System.out.println(("服务端响应" + line));
socket.close(); //关闭流
in.close();
br.close();
标签:socket,创建,br,new,客户端,服务端,out From: https://www.cnblogs.com/460759461-zeze/p/18148618