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

Let me explain, I don't want that you (or other users) write the code for me, but before post to perlmonks I search around the net. So, I'm here because I cannot find anything. :-) What you link is an interesting start point, but I already checked these pages without find a solution to my issue! ;)
  • Comment on Re^4: Check connection state prior to send data

Replies are listed 'Best First'.
Re^5: Check connection state prior to send data
by Corion (Patriarch) on Sep 24, 2016 at 08:07 UTC

    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.

      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.

Re^5: Check connection state prior to send data
by BrowserUk (Patriarch) on Sep 24, 2016 at 08:29 UTC
    What you link is an interesting start point, but I already checked these pages without find a solution to my issue!

    In what way is IO::Select::can_write( [ TIMEOUT ] ); not a solution to your issue?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Ok, maybe I don't understand how the IO::Select works. :(
      Could you please post an example on how I can implement it? Thank you!!

        Adapting the SYNOPSIS gives me the following code:

        use IO::Select; $s = IO::Select->new(); ... $s->add($some_handle); my $outbuffer; while( $running ) { if( length $outbuffer ) { my @writeable = $s->can_write($timeout); for my $client (@writeable) { print {$client} $outbuffer; # In reality, we'd need to maintain an outbuffer per c +lient, but # that's not the point here }; undef $outbuffer; } else { $outbuffer = refill_outbuffer; }; };