import java.net.*;public class TCPServer{ public static void main(String [] args) throw Execption{ ServerSocket ss = new ServerSocket(6000); //这个应用程序具体占用哪个端口 while(true){ Socket s = ss.accept(); //一个服务器可同时连接许多client,与其他client连接的插座 DataInputStream d = new DataInputStream(s.getInputStreaam()); d.readUTF();//阻塞式的,有一个client连接,下一个就不能连接上了 System.out.println(d); d.close(); s.close(); } }}import java.net.*;public class TCPClient{ public static void main(String [] args) throw Execption{ Socket s =new Socket('172.0.0.1',6000);//172.0.0.1是本机IP OutputStream os = s.getOutoutStream(); DataOutputStream d = new DataOutputStream(os); //向管道中写数据 d.writeUTF('hello server'); d.flush(); d.close(); s.close(); } }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。