in reply to Maintain TCP client connection to server?

Tanktalus suggestion is the right path, even if the use of the connected could probably mislead you (i.e. read carefully the documentation).

Thus, you could benefit of the can_write method in IO::Select, even if I actually suspect that your mileage may vary with the system you're using, and in particular whether it supports a "real" select or not.

A simple, brute force approach could be that of trying to write and see if the write was successful. If the TCP connection broke, you should have some feedback about something gone wrong and thus re-establish the connection.

Flavio.

-- Don't fool yourself.

Replies are listed 'Best First'.
Re^2: Maintain TCP client connection to server?
by Anonymous Monk on Mar 25, 2005 at 00:51 UTC
    Hi,

    thanks for info. Well, I have send only TCP connection to server. What I must do is to detect/reconnect tcp connection in less than 2 seconds without writting anything on tcp socket (otherwise alarm will trigger). Is this possible to achieve? Any working example ?

    I've tried connected() and can_write, but both do not detect losing tcp connection (I take out utp cable) but all stays in same state... Methods with reading tcp socket are also useless since I'm not receiveing anything....

    Thanks in advance,

    regards,

    Rob.
      (Please understand that this subthread has become quite Off-Topic here)

      What can_write() can really tell you is if the server has closed its connection in a clean way, i.e. sending you a FIN packet that your TCP stack has diligently ACKed, which is the best you can get now.

      Changing the system won't help you much, so I'm inclined to tell you that you will not know that your connection is down so fast. Any TCP implementation is based on the fact that the underlying network protocol (i.e. IP) can generate delays that are virtually indistinguishable from network failures; in fact, TCP usually adopts some retry schemes for packets that are not acknowledged, so you definitively have to wait.

      Moreover, to check if the server is down you actually have to force the TCP/IP stack to send something to him and have it unacknowledged, and I fear that any implementation is "smart" enough to need real data to make such a check. But here I'm going too deep into the forest, and I don't have the needed light at the moment to make the matter clearer - try to ask to some TCP expert forum, or to read about the TCP implementation in the Linux Kernel.

      Flavio

      Don't fool yourself.

      Note that what you're doing may not even be possible, depending on your operating system (more specifically, depending on the TCP stack you're using). If perl thinks you're still connected, it's likely because the TCP stack thinks you're still connected. And there's not a darned thing you can do about that, other than switch stacks (generally, this means switching OS's).

        Hi,

        thanks for response. Indeed I'm testing this script on Win32, but will definitely use script on Linux systems. Do you think that this will work better under Linux ?

        Regards,

        Rob.