wonder.mice
wonder.mice

Reputation: 7563

When BSD socket reports that RST was received, if not everything was read yet

Lets imagine the following data sequence that was sent from the server to the client:

[data] [data] [data] [FIN] [RST]

And lets imagine that I'm doing the following on the client side (sockets are non-blocking):

char buf[sizeof(data)];
for (;;)
{
    rlen = recv(..., buf, sizeof(buf), ...);
    rerr = errno;
    slen = send(..., "a", 1, ...);
    serr = errno;
}

When I will see the ECONNRESET error?

I'm particularly curious about the following edge case. Let's imagine that all IP frames for the imagined sequence above are already received and ACKed by the TCP stack. However, my client application didn't send() or recv() anything yet. Will the first call to send() return an ECONNRESET? If so, will the next call to recv() succeed and allow me to read everything it has in its internal buffers (since it received the data and has it) before starting to report ECONNRESET (or returning 0 because of FIN)? Or something different will happen?

I will especially appreciate link on the documentation that explains that situation. I'm trying to grok linux tcp implementation to figure that out, but it's not that clear...

Upvotes: 0

Views: 278

Answers (1)

user207421
user207421

Reputation: 310860

Will the first call to send() return an ECONNRESET?

Not unless it blocks for long enough for the peer to detect the incoming packet for the broken connection and return an RST. Most of the time, send will just buffer the data and return.

will the next call to recv() succeed

It depends entirely on (a) whether there is incoming that a to be read and (b) whether an RAT has been received yet.

and allow me to read everything it has in its internal buffers (since it received the data and has it) before starting to report ECONNRESET (or returning 0 because of FIN)?

If an RST is received, all buffered data will be thrown away.

It all depends entirely on the timing and on the size of the buffers at both ends.

Upvotes: 1

Related Questions