Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

IO::Socket recv and send

by milanpwc (Novice)
on Mar 25, 2019 at 03:48 UTC ( #1231643=perlquestion: print w/replies, xml ) Need Help??
milanpwc has asked for the wisdom of the Perl Monks concerning the following question:

When I call recv() or send() from IO::Socket, how can I determine if the remote end has crashed or the connection is broken (ie broken pipe)? When I check the return value, it always return a null string:
while (defined $client_socket->recv($data, 1024)) { + ### always true even if the remote end has crashed print "received data: $data\n"; }

Replies are listed 'Best First'.
Re: IO::Socket recv and send
by thanos1983 (Parson) on Mar 25, 2019 at 08:44 UTC

    Hello milanpwc,

    Welcome to the Monastery. This question has been asked and answered before with different approaches (see here How to find out whether socket is still connected?).

    Another possible way independent the protocol (UDP / TCP) is the module IO::Socket::INET which has the connected method that you can use before sending the data. For example:

    while ( 1 ) { unless ($sock->connected) { $sock->connect($port, $ip) or die $!; } # ... }

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      The connected() method seems to do the trick, but I have two more questions: 1. Do I have to explicitly call it every time before I call send() and recv()? It looks silly. In C, I can immediately tell if the send or recv is successful or not by inspecting the return value. I can also catch SIGPIPE if I need to. 2. What will happen if the peer crashes right after the connected() check and before the send() or recv()? The send() or recv() will still fail and there seems to be no way to avoid this.

        PublicAccess:

        Error checking code often adds a bunch of visual clutter to code. It's all a matter of which tradeoffs you want to make.

        Checking before each send()/recv() is pretty good, but (as you mention) adds clutter. Worse, it won't always work, as you could be disconnected between the check and the following call.

        You could also check only after you receive a null, but only works for recv().

        You mention that in C you could check for SIGPIPE, so if it meets your needs in C, why not use it in your script? The perlvar documentation for %SIG shows how to create a signal handler.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: IO::Socket recv and send
by Anonymous Monk on Mar 25, 2019 at 08:17 UTC
    what kind of a socket? UNIX, UDP, TCP?
      TCP.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1231643]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2019-04-20 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (108 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!