咱们都学过TCP,HTTP的相关概念,本文借助协议分析工具Wireshark,让你们对一些概念眼见为实,权当温故而知新。html
场景:web
在Client(10.239.196.211)上经过web browser访问另外一台Server(10.239.9.22)上的web server. express
步骤:浏览器
0. 首先配置Wireshark -> Edit -> Preference -> Protocol: ide
以下配置的HTTP包显示效果与TCP segment的时序比较一致,便于理解。工具
1. 用浏览器访问 http://10.239.9.22:8080/,同时打开Wireshark, 抓包以下:spa
2. 抓到的内容太多,先用Wireshark filter expression过滤一下: http 3d
3. 而后咱们找到了关心的packet, 右键 Follow -> TCP Stream,找到所在的TCP Stream code
因而获得了以下比较清爽的结果,呈现的便是"HTTP请求所用到的那个TCP connection"(在HTTP 1.1的实现中,同一个TCP Connection能够被多个HTTP通讯复用。)server
4. 一些分析
HTTP 层把下层的TCP的PDU (也就是TCP segment)从新组装成本身的PDU(也就是HTTP message)。在各个协议层,有不一样的PDU,每一个协议层要作2件事情:(i)把本身待发送的PDU交给下层去发送 (ii)把下层的收到PDU拿过来组装成本身的PDU。
TCP只负责发送本身的PDU,也就是TCP segment,TCP会把收到的数据放到本身的buffer里,那么什么时候这些数据会传送给上层的HTTP呢?或者换句话说,HTTP如何知道该用哪些 TCP segment来拼成一个HTTP message呢?这就是PSH flag的做用。观察下图,咱们会看到PSH flag老是出如今一个TCP传输最后一部分的HTTP message时。
关于PSH的权威解释,能够看这个帖子:https://ask.wireshark.org/questions/20423/pshack-wireshark-capture
引用以下:
PSH is an indication by the sender that, if the receiving machine's TCP implementation has not yet provided the data it's received to the code that's reading the data (program, or library used by a program), it should do so at that point. To quote RFC 793, the official specification for TCP:
The data that flows on a connection may be thought of as a stream of octets. The sending user indicates in each SEND call whether the data in that call (and any preceeding calls) should be immediately pushed through to the receiving user by the setting of the PUSH flag.
A sending TCP is allowed to collect data from the sending user and to send that data in segments at its own convenience, until the push function is signaled, then it must send all unsent data. When a receiving TCP sees the PUSH flag, it must not wait for more data from the sending TCP before passing the data to the receiving process.
There is no necessary relationship between push functions and segment boundaries. The data in any particular segment may be the result of a single SEND call, in whole or part, or of multiple SEND calls.
The purpose of push function and the PUSH flag is to push data through from the sending user to the receiving user. It does not provide a record service.
咱们实验中的web server端的HTTP层就至关于引文中的sending user,实验中的web browser端的HTTP层就至关于引文中的receving process。
PSH flag的做用有2方面:
所以,web server所在机器的TCP协议在发送一个HTTP reponse时,当它意识到这是最后一个TCP segment时,就会加上PSH flag,而后全部buffer中还没有发送的数据都会被发送出去;而web browser所在机器的TCP协议在看到一个TCP segment带有PSH flag时,就会意识到已经收到足够信息了,应当当即把到目前为止已经收到的信息交给上层应用(即HTTP协议)。
简单的说,PSH flag有点像咱们写文件时的flush操做。
还有几点,稍微提一下: