1.TCP/IP+BIO

即TCP协议下阻塞式传输

客户端:

@SuppressWarnings("resource")
Socket socket = new Socket("127.0.0.1", 8989);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
out.println("hello");

服务端:

ServerSocket ss = new ServerSocket(8989);
ss.setSoTimeout(10000);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String info = br.readLine();
System.out.println("Server receive info: " + info);

代码缺点:
服务端不能同时接收多个连接请求。

你可能感兴趣的:(1.TCP/IP+BIO)