I have been trying to figure out how to select() a socket so that if in a specified timeout I can attempt to make a connection to a remote host or not. Here is what I have and it always fails!
my $proto = getprotobyname('tcp');
my $ipaddr = inet_aton($host);
my $portaddr = sockaddr_in($port, $ipaddr);
socket(SOCK, PF_INET, SOCK_STREAM, $proto);
print "Trying to Connect...\n";
my ($rin, $rout, $win, $wout, $ein, $eout) =('', '', '', '', '', '');
vec($rin,fileno(SOCK),1) = 1;
my($nfound, $timeleft) = select($rout=$rin, $wout=$win, $eout=$ein, 2);
if($nfound == 0) {
print "Timeout!\n";
exit(0);
}
connect(SOCK, $portaddr);
From what I can tell, I'm doing everything right, but it alway fails (i.e. "Timeout!"
. I have removed error checking from the above code to make it easier to read, but assume that the $host, and $port are valid, and the host is alive.
Thanks,
-bitwise
PS: I've tried the "IO::Select" interface but that didn't work either, and I would prefer to do it manually. Thanks.
my $proto = getprotobyname('tcp');
my $ipaddr = inet_aton($host);
my $portaddr = sockaddr_in($port, $ipaddr);
socket(SOCK, PF_INET, SOCK_STREAM, $proto);
print "Trying to Connect...\n";
my ($rin, $rout, $win, $wout, $ein, $eout) =('', '', '', '', '', '');
vec($rin,fileno(SOCK),1) = 1;
my($nfound, $timeleft) = select($rout=$rin, $wout=$win, $eout=$ein, 2);
if($nfound == 0) {
print "Timeout!\n";
exit(0);
}
connect(SOCK, $portaddr);
From what I can tell, I'm doing everything right, but it alway fails (i.e. "Timeout!"
Thanks,
-bitwise
PS: I've tried the "IO::Select" interface but that didn't work either, and I would prefer to do it manually. Thanks.