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?

In reply to IO::Socket::INET Dangling Connections by simeon2000

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.