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 packe开发者_如何学编程ts are fragmented into smaller parts. I'm transferring over the HTTP protocol.
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.
You might use PSH
flag of TCP
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 your HTTP GET
is marked by this flag.
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 return 0
. This will happen when the other side closes the connection (which it never has to do).
精彩评论