simeon2000 has asked for the wisdom of the Perl Monks concerning the following question:

I recently wrote an IO::Socket:INET server in perl. It runs just fine, and does what I want it to... but I've hit a small snag; dangling connections. Some of the clients don't exit properly, or just leave the connection to the forked socket open.

What I'd like to do is to say that if I don't recieve input from the client in, say, 10 seconds, to close the socket. I've tried setting "Timeout" on the listening socket, but all that seems to do is close the listen socket if it doesn't recieve a connection after 10 seconds, which is obviously not what I want.

Here's an example of the code, and what I want to do in comments:

# For each connection we accept while (my $con = $lsock->accept) { # This holdes the Child PID I believe my $child; # Fork a kid or die baby die "Can't fork: $!\n" unless defined ($child = fork()); if ($child == 0) { # Close the child's listen socket, we don't need it $lsock->close if $lsock; ### # Here's the problem I believe. On while $line = <$con> # I'd like to do "last if no data recieved for $x seconds." ### # For each line of input from the connection while (my $line = <$con>) { # remove newlines, bad data, etc. $line = filter($line); unless ($line) { print $con $error; next } # I determine what to send back by what they send in if ($line eq 'QUIT' ) { print $con "+OK Sayonara\n"; last; } elsif ($line eq 'GET DATA') { print $con "+OK Sending DATA\n", $lib->get_data, "\n.\n"; next; } else { print $con $error; next } } # Close this connection $con->close if $con; exit(0); } else { # We're the parent, we've already handed the connection off $con->close; } }
I'm positive there's a module or something out there to help me with this. Would someone please guide this poor soul in the right direction?

Replies are listed 'Best First'.
Re: IO::Socket::INET Dangling Connections
by rpc (Monk) on Feb 21, 2001 at 00:24 UTC
    IO::Select is what you need. This will allow you to perform a non-blocking wait on the socket.
    use IO::Select; use constant TIMEOUT => 10; ... ... my $select = IO::Select->new(); $select->add($con); # from your example above. # Don't need to save the array returned, there's only one handle. if($select->can_read(TIMEOUT)) { # $con has pending data. } else { # timeout. }
      Thank you very much for that reply. I'm now looking into the docs on IO::Select... it's an interesting module. I'm going to look into it and see if I can figure it out from here. My data processing routines will have to be modified, but I htink I can handle it from here. Thanks so much.