三次握手创建TCP之后开始传输数据。html
Accept后对于服务端来讲整个socket建立完毕,直接进入read状态。read是一个阻塞调用,所谓阻塞是指服务器进入等待,直到read返回。java
read实际上是的主要时间是等待数据ready:api
因此read阻塞的时间至少须要T1+T2+T3,其中T1+T2的时间为等待数据的时间服务器
接下来用java源码来演示BIO的过程,为了看到效果特地在客户端加入了两次sleep来标识客户端IO的时间,而服务端的read却要一直等待阻塞在这个客户端的IO上。socket
源码参考async
import java.io.InputStream; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; public class SocketBioServer { static SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); public static void main(String[] args) { try { ServerSocket serverSocket = new ServerSocket(); serverSocket.bind(new InetSocketAddress("localhost", 8080)); while (true) { Socket socket = serverSocket.accept(); System.out.println(time() + "->accepted, begin to read......"); String result = readBytes(socket.getInputStream()); System.out.println(time() + "->" + result); socket.close(); } } catch (Exception ex) { ex.printStackTrace(); } } static String readBytes(InputStream is) throws Exception { long start = 0; int total = 0; long begin = System.currentTimeMillis(); int count = 0; while ((count = is.read()) > -1) {//block,有数据写入的时候才会返回值。客户端关闭后才会返回-1 if (start == 0) { start = System.currentTimeMillis(); } total += count; } //读完数据的时间 long end = System.currentTimeMillis(); return "wait=" + (start - begin) + "ms,read=" + (end - start) + "ms,total=" + total + "bs"; } static String time() { return sdf.format(new Date()); } }
import java.net.InetSocketAddress; import java.net.Socket; public class SocketBioClient { public static void main(String[] args) { try { Socket s = new Socket(); s.connect(new InetSocketAddress("localhost", 8080)); Thread.sleep(5000);//模拟数据发送前的等待 s.getOutputStream().write(prepareBytes()); Thread.sleep(1000);//模拟写入数据须要的时间 s.close();//关闭后客户端才能返回-1 } catch (Exception ex) { ex.printStackTrace(); } } static byte[] prepareBytes() { byte[] bytes = new byte[1024 * 1024 * 1]; for (int i = 0; i < bytes.length; i++) { bytes[i] = 1; } return bytes; } }
23:03:14->accepted, begin to read...... 23:03:20->wait=5005ms,read=1002ms,total=1048576bs
经过上述能够看到read一直阻塞等待客户端的IO写入。tcp