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!

How do you handle a timeout with IO::Select->

Status
Not open for further replies.

JeffHoeft

Programmer
Jun 26, 2003
13
US
Hello!

I am trying to understand the best way to tell if a socket connection read operation has timed out. I have include snippets of a much bigger program to best explain my delema.
Do i have to test for an empty array or will the program exit with an error status? or something else all together.

fyi $CONF is a configuration file holding values for the program.


### Open a server socket and await incoming connections.
### The IO::Socket::INET module is used instead of theSocket
### module to simplify the code.
my $sockfd = IO::Socket::INET->new(LocalPort => $CONF::port,
Type => SOCK_STREAM,
Reuse => 1,
Listen => SOMAXCONN)
or die ("MAIN FATAL: Could not open TCP socket on port $CONF::port: $@\n");


### Await incoming connections
CONNECTION:
while (my $connfd = $sockfd->accept())
{
### Fork a child process
if ((my $chldpid = fork()) != 0)
{
###This is the parent. Close the connected socket
###and await the next client connection.
close ($connfd);
next CONNECTION;
}


### The child will close listening socket.
close ($sockfd);

### Use select to place a time out on socket reads
my $select = IO::Select->new();
$select->add($connfd);
my @read_from = $select->can_read($CONF::timeout);
$connfd = $read_from[0];

### What happens here if the connection times out
### Do i have to test for something?

(etc...)

}
 
can_read will return an empty array on timeout. So if scalar @read_from == 0 the select has timed out with no handles readable.

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top