Send buffer empty of Socket in Linux?

后端 未结 1 1209
臣服心动
臣服心动 2021-01-23 11:19

Is there a way to check if the send buffer of an TCP Connection is completely empty? I haven\'t found anything until now and just want to make sure a connection is not closed by

相关标签:
1条回答
  • 2021-01-23 11:46

    In Linux, you can query a socket's send queue with ioctl(sd, SIOCOUTQ, &bytes). See man ioctl for details.

    The information is not completely reliable in the sense that it is possible that the data has been received by the remote host, since the buffer cannot be emptied until an ACK is received. You probably should not use it to add another level of flow-control on top of TCP.

    If the remote host actually closes the connection (or half-closes it), then the socket become unwriteable, regardless of how much data might have been in the buffer. You can detect this condition by writing 0 bytes to the socket.

    The more difficult (and often more likely) condition is the remote host becoming unreachable, because of network issues or because it crashes. In that case, data will pile up in the send buffer, but that can also happen because the remote host's receive buffer is full (perhaps because the process reading the buffer doesn't have enough resources to process its input). In the case of network routing issues, you might get a router notification (an ICMP error), which should make the socket unwritable; unfortunately, there are many network errors which just result in black holes.

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