Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by saurabh.hirani (Beadle)
on May 05, 2009 at 15:47 UTC ( [id://761968]=note: print w/replies, xml ) Need Help??


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

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?

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

Replies are listed 'Best First'.
Re^3: how to set socket recv timeout in architecture independent manner?
by JavaFan (Canon) on May 05, 2009 at 21:41 UTC
    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?

      Are you sure that works on all platforms?

      I don't know. I tried looking it up but could not find any pointers on it being architecture dependent. But to be sure, I can use a non blocking recv() instead of sysread() because the only time I would get to do the read operation (be it through recv() or sysread()) would be when select has given me something to read. That way my recv() wouldn't block. Is sysread architecture dependent?

      And while I was looking through the monastery I found this interesting post about sysread - sysread and syswrite in tcp sockets - which says sysread attempts to read the no. of bytes it was told to.

      Update: Read the recv function from perldoc - it exhibits similar behaviour - it too attempts to read the no. of bytes it was told to.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://761968]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2024-04-24 16:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found