Upon receiving a TCP RST packet, will the host drop all the remaining data in the receive buffer that has already been ACKed by the remote host but not read by the application process using the socket?
I’m wondering if it’s dangerous to close a socket as soon as I’m not interested in what the other host has to say anymore (e.g. to conserver resources); e.g. if that could cause the other party to lose any data I’ve already sent, but he has not yet read.
Should RSTs generally be avoided and indicate a complete, bidirectional failure of communication, or are they a relatively safe way to unidirectionally force a connection teardown as in the example above?
Application-level
close(2)on a socket does not produce anRSTbut aFINpacket sent to the other side, which results in normal four-way connection tear-down.RSTs are generated by the network stack in response to packets targeting not-existing TCP connection.On the other hand, if you close the socket but the other side still has some data to write, its next
send(2)will result inEPIPE.With all of the above in mind, you are much better off designing your own protocol on top of TCP that includes explicit “logout” or “disconnect” message.