Your question is a bit vague. Saying "it stops responding" could mean several things. Perhaps zentara's links will solve your problem.

I've written an example TCP server which will kill a process after $timeout seconds or if the client closes the connection.

You can test it with a web browser, e.g., connecting to http://yourserverhere:5050/ and then clicking the Stop loading button.

If neither my nor zentara's answer helps you, post your actual code and a more precise description of what happens. (By the way, the correct English is "timing out", not "timeouting".)

use IO::Socket::INET; use IO::Select; use Errno; use strict; $| = 1; my $timeout = 30; my $listen = IO::Socket::INET->new( Listen => 5, LocalPort => 5050, Proto => "tcp") or die "new sock error: $!\n"; my $alive; $SIG{CHLD} = sub { $alive = 0 }; while (1) { my $conn = $listen->accept; if (!$conn) { if ($!{EINTR}) { next } else { last } } print "Accepted\n"; while (<$conn>) { last if /^\r?\n$/; } print "After header\n"; $alive = 1; my $pid = fork; die "fork error: $!" unless defined $pid; if ($pid) { IO::Select->new($conn)->can_read($timeout); if ($alive) { print "killing child process\n"; kill 'KILL', $pid } else { print "Child already dead\n"; } } else { print $conn "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n"; print $conn "<html><head><title>abort test</title></head>\n"; for (1 .. 100) { print $conn "<div>$_<br></div>\n"; sleep 1; } print $conn "</html>\r\n"; $conn->shutdown(2); close($conn); } }

In reply to Re: Timeouting a socket script by Thelonius
in thread Timeouting a socket script by ivanatora

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.