On 2012-02-13 16:50, R.Wieser wrote:
> I've made a program (a client of sorts) which creates a TCP/IP connection to
> a server, and keeps it open for hours. Normally this works alright, but
> sometimes something happens and the connection goes dead.
>
> The problem is that at my side the socket seems to keeps running, there is
> just not coming any data thru it anymore, not even the heartbeat-signal.
>
What's "the heartbeat-signal"? The only thing TCP/IP has akin to a heartbeat
signal is TCP keepalives -- by default this is not enabled, and even if you
do, the default interval is two hours, and even if you take that into
account, the only thing it does is send out 0-byte packets outside the
window (or a packet containing a single NUL byte, I forget which one it is
on Windows) that may or may not be processed correctly by intermediate
firewalls and the server itself. In other words, it's not as useful as you
might like.
> My question: is there something more I can add to that to get a "connection
> dropped" event (or alike) ?
>
> If not, can I create a hearbeat signal myself *without* actually sending
> data to the program at the other side (something captured and echo-ed back
> by the TCP/IP stack would be fine though).
>
In short: no. It's a feature of TCP/IP that connections can survive
interruptions. If you rather need guarantees about the availability of the
connection, your protocol must account for this, and most do this by either
an explicit keepalive package format or an invalid request that must be
responded to.
A crude alternative is to assume that any connection that hasn't seen any
activity for a period of time is no longer reliable, and rebuild it. For
this alternative, the "period of time" ought to be longer than the time-wait
delay (4 minutes by default), otherwise you can run out of sockets if the
other side remains unresponsive and you keep creating connections.
--
J.