package com.sxt.tcp; import java.io.DataInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { System.out.println("----Server----"); ServerSocket server = new ServerSocket(8888); Socket client = server.accept(); System.out.println("一个客户建立连接"); DataInputStream dis = new DataInputStream(client.getInputStream()); String recv = dis.readUTF(); System.out.println(recv); dis.close(); client.close(); server.close(); } }
package com.sxt.tcp; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; public class Client { public static void main(String[] args) throws UnknownHostException, IOException { System.out.println("----Client----"); Socket client = new Socket("localhost",8888); DataOutputStream dos = new DataOutputStream(client.getOutputStream()); String data = "你好呀,TCP"; dos.writeUTF(data); dos.flush(); dos.close(); client.close(); } }
结果
原文地址:https://www.cnblogs.com/nihaoya-czw/p/10353509.html
时间: 2024-10-12 12:17:57