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.
| [reply] |
|
Thanks for the pointers. I will check them out.
| [reply] |
|
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.
| [reply] |
|
overkill? Maybe using a computer is overkill :D
| [reply] |
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. | [reply] [d/l] |
|
#!/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?
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
Re: how to set socket recv timeout in architecture independent manner?
by BrowserUk (Patriarch) on May 06, 2009 at 07:59 UTC
|
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.
| [reply] [d/l] |
|
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) );
| [reply] [d/l] |
|
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
| [reply] |
|
|
|
|
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.
| [reply] |
|
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:
- 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.
- 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.
| [reply] [d/l] |
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);
| [reply] [d/l] |
|
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.
| [reply] |