in reply to how to set socket recv timeout in architecture independent manner?

Try:

setsockopt( $client, SOL_SOCKET, SO_RCVTIMEO, pack('!L!L', +10, 0) );

You will need to ajdust the pack statement to suit the platform. See pack docs for details.

Here is the setsockopt() docs for windows.


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re: how to set socket recv timeout in architecture independent manner?
  • Download Code

Replies are listed 'Best First'.
Re^2: how to set socket recv timeout in architecture independent manner?
by robnewton (Initiate) on Jun 15, 2009 at 23:03 UTC
    Great, your suggestion to use the '!' modifier in pack() made my code work for 32 and 64-bit platforms. However, the '!' must come AFTER the 'L', giving:

     setsockopt( $client, SOL_SOCKET, SO_RCVTIMEO, pack('L!L!', +10, 0) );

      I guess another way to tackle the problem using pure Perl internals would be setting an alarm for the recv operation. This may not be most elegant piece of code but can solve a quick script.

      Just FYI I am using Cygwin and SO_RCVTIMEO does not work very good. I am on winXP (32bit) and tried ll, LL, qq, QQ

        Did you try 'VV'?


        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.
Re^2: how to set socket recv timeout in architecture independent manner?
by saurabh.hirani (Beadle) on May 06, 2009 at 16:41 UTC
    Thanks. But if you read the first post of this thread, I have used sockopt and I wanted to do this in an architecture independent manner. Read the follow ups to know how it was accomplished with valuable inputs from the monks.

      Well okay, I'm glad you got what works for you, but ... in your orignal post you asked for how to

      set recv timeout ... architecture independent way

      That generally means there are two problems to solve:

      1. Are the system functions available to do X.

        And ignoring obscure little used platforms, that usually divides into POSIX and Win platforms. As setsockopt is a POSIX core function that takes care of most places. My post was to show you that it is available on windows also.

      2. Passing architecture-dependant binary values.

        By using a '!L', you get an architecture independant way of native longs.

      Whilst using select and can_read() with timeout achieves a similar goal, you are substituting a 'polling' solution for a timer solution (at least in some platforms), and that can significantly increase you cpu usage.

      In addition, using select imposes a particular architecture--which is often unnatural--upon your application. If your happy with that, then great, but I thought the alternative worth a mention.


      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.