in reply to Re^4: Check connection state prior to send data
in thread Check connection state prior to send data

If the socket is not connected, writing to it will return an error which you can then check. Usually, writing to a socket has a timeout which you cannot avoid due to the nature of TCP.

Personally, I would look at AnyEvent or one of the other frameworks to handle non-blocking sockets.

Maybe you can tell us what solutions you have looked at and where you have problems with them? If you want to roll your own solution, perlipc provides a good starting point IMO.

  • Comment on Re^5: Check connection state prior to send data

Replies are listed 'Best First'.
Re^6: Check connection state prior to send data
by Lucas Rey (Sexton) on Sep 24, 2016 at 09:16 UTC
    Could you please tell me how intercept the error coming from socket? Because I tried, the following:
    ... OpenSocket(); sleep(5); $str="Something to send"; $sock1->send($str);
    Then, during sleep I disconnect acting on server. When the application try to send the message, it exit from execution without print anything.

      Maybe it would be a good idea to check the return value of ->send? The following program behaves as I expect, telling me that the remote end has closed the connection:

      #!/usr/bin/perl use IO::Socket; my $sock1; sub OpenSocket{ $sock1 = IO::Socket::INET->new( PeerAddr => '192.168.1.99', PeerPort => 80, Proto => 'tcp' ); $sock1 or die "no socket :$!"; } ################ START SCRIPT ################# OpenSocket(); $str="Something to send"; while (1) { if( !$sock1->send($str)) { warn "Error: $! / $^E"; }; } close($sock1);

      I'm not sure if the autodie pragma also covers IO::Socket. If it does, maybe you want to use it, if not, you will have to check every function call you make yourself for success/failure.