Writing on a TCP socket closed by the peer

前端 未结 3 416
北海茫月
北海茫月 2020-12-11 07:10

I have a client-server application where each side communicate with the other via TCP socket.

I properly establish the connection and then I crash the server BEFORE

相关标签:
3条回答
  • 2020-12-11 07:37

    This has to do with how TCP/IP works, that can be roughly described as two mostly independent half-connections. When you close the socket at the server, the client is told that it will not receive further data from the C<-S half-connection, waking up read() immediatly, but not about the C->S direction. It only gets a reply resetting the connection after it tries to send some data. I recommend the TCP/IP Guide for further details.

    The reason why sometimes you can write() twice is that you write faster than the round-trip time and can squeeze a second write() before the reply to the first one.

    0 讨论(0)
  • 2020-12-11 07:42

    You're confused by what the return value of write() means. It doesn't mean, "the peer got the data and acknowledged it". Instead, it means, "I buffered so-many bytes to send to the peer and they're my responsibility now, so you can forget about them (and I don't have any pending errors)".

    That is, if the TCP stack accepts the write and returns n bytes, that doesn't mean they've been written yet, just queued for writing. It'll take some time, perhaps 30s after it starts sending network traffic, before the stack gives up and returns an error to you. During that time, you could have done several calls to write() which were successful at queueing data for sending. (The write error will be returned in c.30s if the peer has vanished, or immediately if the peer can be contacted and sends a RST packet straight away to indicate the connection is dead.)

    0 讨论(0)
  • 2020-12-11 07:45

    I'm using the following method to detect a disconnected server condition:

    After getting the select() timeout on a socket (nothing was received, though was supposed to), the 'system("ping -c 1 -w 1 server");' command is activated. If the server is up and just lagging, the ping command will return in less than 0.1 seconds. Otherwise (the server is down), the ping command will return in 1 second.

    0 讨论(0)
提交回复
热议问题