in reply to function to count pending packets in TCP socket
Thank you for your replies and trying to help me. Please also forgive that my language is not always clear. I try to improve.
I am speaking about TCP and not about UDP. Many think that data cannot be lost on TPC, unfortunately this is not always true. As long as both peers keep their sockets open and only the network is down for some time, the connection will recover, data transmission is repeated and nothing is lost. If, however, one side dies, the socket on the other side may still be open, so the application may continue writing data into this socket until it will block or eventually returns an error code. And obviously all this data will never arrive at the other side which is already dead.
Testing $conn->connected() exactly fails in this sitation of a "half-open" TCP connection, since the local socket believes the connection is established. That is exactly the issue I want to detect
It has been suggested to use SO_KEEPALIVE. Thank you for this idea. This indeed would resolve the issue, however it is not a viable solution. The problem here is, that the keepalive timeout typically is 2 hours. As far as I know, it can only be configured globally on a machine, so I don't want to play with this parameter, as it may cause side effects. I want to detect the half-open connection after a few seconds, ont only after hours. Some comments on the web suggest that the application should use its own heartbeat messages. However this will not work in my case. Unfortunately the application protocol is fixed, I cannot change it, and it does not provide a heartbeat message
Pinging the peer hoat may be another way to check, if it is available. However, I would prefer send_q as it would use status data of the actual socket rather than introducing another channel.
Thank you also for pointing out that send_q does not count the pending messages but the pending octets. Good to know, even though in my case this detail does not really matter. I would only be interested to test if send_q is 0 or not.
Now I come back to my original question: Is there a function in Perl which returns the current value of send_q for an open TCP connection? Would be great, if someone knows the answer. Thanks again for your support. Best regards, Andreas
I should add that in my case it would be fully sufficient if the application layer detects that something is "wrong" with that TCP connection. There are recovery mechanisms available in the code which only need to be triggered.