Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select on a socket and timeout

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top