本文主要是介绍[Java] 网络-01 TCPClient / TCPServer,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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();}
}
这篇关于[Java] 网络-01 TCPClient / TCPServer的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!