http://www.perlmonks.org?node_id=1000702

tenny1204 has asked for the wisdom of the Perl Monks concerning the following question:

I wrote some socket-communication programs and now I want to rewrite them using the built-in functions iso the IO::Socket library.But i can't figure out how to set a timeout like you can do in the constructor when using IO::Socket.What i tried was adding these 2 lines of code for setting a 1 second timeout.

setsockopt(SOCKET,SOL_SOCKET,SO_SNDTIMEO,pack("LL",1,0)); setsockopt(SOCKET,SOL_SOCKET,SO_RCVTIMEO,pack("LL",1,0));

They don't work and i can't find out what does. Maybe someone can help me, Thank you in advance

Replies are listed 'Best First'.
Re: built-in socket functions
by Anonymous Monk on Oct 25, 2012 at 02:33 UTC

    What kind of socket do you have?

    Doesn't SO_SNDTIMEO require blocking sockets?

    Why aren't you error checking?

    setsockopt ... or die Fudge("SO_SNDTIMEO"); setsockopt ... or die Fudge("SO_RCVTIMEO"); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }

      I tried errorchecking but no error, doesn't even seem to matter what i write after pack().I have something like:

      $port=80; $proto=getprotobyname('tcp'); socket(SOCKET,AF_INET,SOCK_STREAM,$proto) or die "no socket"; setsockopt(SOCKET,SOL_SOCKET,SO_RCVTIMEO,pack("LL",1,0)); $iadr = inet_aton("127.0.0.1"); $padr=sockaddr_in($port,$iadr); if(connect(SOCKET,$padr)){ print "connection ok";} else{print "$!\n";}

      It should correspond to something like:

      $conn = IO::Socket::INET->new( PeerAddr=>"127.0.0.1", PeerPort=>80, Proto=>'tcp', Timeout=> 1, Blocking=>1 );

      So: a blocking socket with a 1 second timeout because now the standardtimeout is something like 10 seconds,making my applications run slower iso faster which was the general idea.

        See Re: how to set socket recv timeout in architecture independent manner?


        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".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        RIP Neil Armstrong

Re: built-in socket functions
by zentara (Archbishop) on Oct 25, 2012 at 10:42 UTC
    If you havn't seen it, cookbook example

    They use a syntax like

    use Socket; require "sys/socket.ph"; # for &TCP_NODELAY setsockopt(SERVER, SOL_SOCKET, &TCP_NODELAY, 1) or die "Couldn't disable Nagle's algorithm: $!\n"; Re-enable it with: setsockopt(SERVER, SOL_SOCKET, &TCP_NODELAY, 0) or die "Couldn't enable Nagle's algorithm: $!\n";

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh