Is it wise/safe to close()
a socket directly after the last send()
?
I know that TCP is supposed to try to deliver all remaining data in the send buffer even after closing the socket, but can I really count on that?
I'm making sure that there is no remaining data in my receive buffer so that no RST will be sent following my close.
In my case, the close is actually the very last statement of code before calling exit()
.
Will the TCP stack really continue to try and transmit the data even after the process sending it has terminated? Is that as reliable as waiting for an arbitrary timeout myself before calling close()
by setting SO_LINGER?
That is, do the same TCP timeouts apply, or are they shorter? With a big send buffer and a slow connection, the time to actually transfer all the buffered data could be substantial, after all.
I'm not interested at all in being notified of the last byte sent; I just want them to eventually arrive at the remote host as reliably as possible.
Application layer acknowledgements are not an option (the protocol is HTTP, and I'm writing a small server).
close()
will flush any data remaining in the buffer before it actually tears down the socket. Mileage may vary if you terminate the entire process in addition to callingclose()
.close()
. Will the call succeed in that case? I doubt it. Granted, what you say should hold true for any single-threaded program. But not all programs are single-threaded.