当前位置: 代码迷 >> 综合 >> [Java] 网络-01 TCPClient / TCPServer
  详细解决方案

[Java] 网络-01 TCPClient / TCPServer

热度:89   发布时间:2023-12-14 09:19:16.0
import java.net.*;
import java.io.*;public class TCPServer {public static void main(String[] args) throws Exception {ServerSocket ss = new ServerSocket(6666);while(true) {Socket s = ss.accept(); // accept 阻塞式的
System.out.println("a client connect!");DataInputStream dis = new DataInputStream(s.getInputStream());System.out.println(dis.readUTF()); // readUTF 阻塞式的dis.close();s.close();}}
}
import java.net.*;
import java.io.*;public class TCPClient {public static void main(String[] args) throws Exception {Socket s = new Socket("127.0.0.1", 6666);OutputStream os = s.getOutputStream();DataOutputStream dos = new DataOutputStream(os);Thread.sleep(30000);dos.writeUTF("hello server!");dos.flush();dos.close();s.close();}
}


  相关解决方案