数据传输时如何知道服务器收到的最后一个TCP报文段是哪一个?
当使用 TCP 传输数据时,给定所有传入和传出的数据包,如何知道收到的数据包是否是最后一个数据?
TCP 数据包被分成更小的部分。我正在通过 HTTP 协议进行传输。
When transferring data in TCP, and given all the incoming and outcoming packets, how will one know if the packet received is the last of the data?
TCP packets are fragmented into smaller parts. I'm transferring over the HTTP protocol.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
TCP
协议的PSH
标志。在最后一个数据包中应将其设置为 1。要确认这一点,只需开始跟踪,进行 HTTP GET 并过滤会话。您会发现
HTTP GET
上每个响应的最后一个数据包都标有此标志。You might use
PSH
flag ofTCP
protocol. It should be set to 1 in last packet.To confirm this just start tracing, make
HTTP GET
and filter session. You will find that last packet for each response on yourHTTP GET
is marked by this flag.当连接的一端设置 FIN 标志时,表示该端将不再发送数据。
如果在最后一个数据之后连接没有关闭,那么必须有一个应用程序层方法来确定它。对于 HTTP,规则相当复杂。
When the
FIN
flag is set by one end of the connection, it indicates that that end will not be sending anymore data.If the connection is not being closed after the last of the data, then there must be an application-layer method of determining it. For HTTP, the rules are reasonably complicated.
我假设您正在使用某种套接字库。您可以判断 TCP 连接已完成,因为套接字上的
read()
将返回0
。当另一方关闭连接时就会发生这种情况(它永远不必这样做)。I'm assuming you're using some sort of socket library. You can tell a TCP connection is finished because a
read()
on the socket will return0
. This will happen when the other side closes the connection (which it never has to do).