saurabh.hirani has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I am trying to set recv timeout for a udp socket created through IO::Socket::INET. As I read, the method to do this is

my $timeout = pack("qq", 10, 0); $sock->sockopt(SO_RCVTIMEO, $timeout);

"qq" denotes signed 64 bit value as my machine has 64 bit architecture. If I am on a 32 bit machine, "qq" should be replaced by "ll". I can check for x86_64 and i(3|4|5|6)86 but then, there would be ppc64, ia64, etc. architectures too.

Is there an architecture independent way to do this? Or is there a better way to set recv timeout?

thanks and regards,

Saurabh.

  • Comment on how to set socket recv timeout in architecture independent manner?
  • Download Code

Replies are listed 'Best First'.
Re: how to set socket recv timeout in architecture independent manner?
by jettero (Monsignor) on May 05, 2009 at 13:51 UTC
    Don't do it by hand. That's your best bet. Try something like POE or Net::Server. That's not directly answering your question, but it probably addresses your application as a whole. There are actually a lot of really good choices for handling sockets for you and writing it yourself usually causes more headaches than it's worth.

    -Paul

      Thanks for the pointers. I will check them out.

      Net::Server or POE look like great tools but they would be an overkill for such a small operation. From what I saw, they might be useful for building a more complex app or a generic base. But for a listen-reply server IO::Socket::INET gives all that is needed, which is not much - send and recv.

      Also there are a many other programs of mine which need to have this feature, all of which are already using IO::Socket::INET.

        overkill? Maybe using a computer is overkill :D
Re: how to set socket recv timeout in architecture independent manner?
by JavaFan (Canon) on May 05, 2009 at 14:27 UTC
    The classical way is to use a 4-arg select call, with the 10 second timeout as the fourth argument.

      Thanks Javafan. Using IO::Select is a good idea.

      #!/usr/bin/perl -w # # udp client which sends a query, waits for timeout # seconds and prints response if obtained else cribs. # use strict; use IO::Socket; use IO::Select; my $srvhost = '10.1.1.111'; my $srvport = '3333'; my $client = new IO::Socket::INET( PeerAddr => $srvhost, PeerPort => $srvport, Proto => 'udp'); my $sel = new IO::Select($client); my @ready; my $sock; my $request = "e8aa9e somequery 123"; my $maxread = 1024; while (1) { if (! defined($client->send($request))) { die "failed to send req\n"; } print "sent $request\n\n"; @ready = $sel->can_read(5); if (! scalar(@ready)) { print "Response Timed out\n"; } else { $sock = $ready[0]; if (! sysread($ready[0], $response, $maxread)) { print "recv failed :$!\n"; } else { print "got response $response\n"; } } sleep 3; }

      I don't need to do recv as I can read through sysread, and this being a server response I don't need to send back any replies - so I don't need recv for its capabilities of getting the remote host's information. I don't even need to be in blocking mode now - or do I?. Am I right in saying this?

        I don't need to do recv as I can read through sysread
        I'm a bit surprised. A few posts ago, you're wondering how to pass the number 10 in a platform independent way, and now you want to use sysread instead of recv to read from a socket.

        Are you sure that works on all platforms?

Re: how to set socket recv timeout in architecture independent manner?
by BrowserUk (Patriarch) on May 06, 2009 at 07:59 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

      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.
Re: how to set socket recv timeout in architecture independent manner?
by JavaFan (Canon) on May 05, 2009 at 13:57 UTC
    my $sock = IO::Socket::INET->new(...., Timeout => 10);
      Even I thought of using 'Timeout' at first. But little did I know that Timeout in IO::Socket::INET is used during connect() and accept() calls and I need to set timeout for recv() system call. Timeout won't help me.