Hi aaronwroblewski,

I would approach the problem using the following steps (maybe not in exactly the same order):

HTH

Update (in response to Re^2: IO::Socket Not responding after period of time and traffic!):
Sorry, when I meant get rid of while(1), I was possibly not precise enough. You still need an infinite loop to accept() new connections, but you do not need to create/kill the server socket itself. See the following annotated modifications to your script... I used #-- for removed lines, #++ for lines added, and #!! to indicate your shame ;-)

#!/usr/bin/perl #!! use strict; #!! use warnings; use IO::Socket; use IO::Handle; use Sys::Hostname; use POSIX qw(:sys_wait_h); #++ see advice of other monks regarding REAP (not applied here) #++2 just to clarify, THIS is YOUR version of sub REAP ;-) (well, warn +(..) added for debugging purpose) #++3 please study "perldoc perlipc" carefully - it holds a full sample + of a spawning server, that you #++3 could easily adapt - or see CPAN Net::Server (i.e.) sub REAP { warn("REAP... \n"); #++ 1 until (-1 == waitpid(-1, WNOHANG)); $SIG{CHLD} = \&REAP; } $SIG{CHLD} = \&REAP; #--(this would create/destroy the sever socket - don't!) #-- while (1) { my $sock = new IO::Socket::INET ( LocalHost => 'localhost', LocalPort => '1818', Proto => 'tcp', Listen => 255, Reuse => 1, Timeout => '15',); $sock or die "no socket :$!"; STDOUT->autoflush(1); my($new_sock, $buf, $kid); #-- while ($new_sock = $sock->accept()) { while (1) { #++ endlessly accept new connections #++ accept() blocks or timeouts after 15s (here) warn ("Sever waiting...\n"); #++ if ($new_sock = $sock->accept()) { #++ $new_sock->autoflush(1); warn "New client...\n"; #++ # execute a fork, if this is # the parent, its work is done, # go straight to continue next if $kid = fork; die "fork: $!" unless defined $kid; # child now... # close the server - not needed #--(you want the server socket to survive!) #-- close $sock ; warn("Child ($$) reading...\n"); #++ while (defined($buf = <$new_sock>)) { #---- removed distracting stuff #++ minimum functionality added print $new_sock "ECHO ($$): $buf\n"; #++ last if $buf =~ /quit/; #++ $new_sock->flush; #-- Alas! Code formatting, Grashopper! #-- } } warn "Child ($$) quits.\n"; #++ close $new_sock; #++ (well, explicite here since exit() would clos +e() it for you...) exit; #-- } continue { # parent closes the client since # it is not needed #-- close $new_sock; } else { #++ #++ Check if a problem occured or a signal was received...(see per +lipc). #++ Since Timeout=>15, this will be visited every 15s! Chose Timeo +ut=>undef #++ to block the server unless a client wants to connect (is accep +t()'ed). warn "Server: $!\n"; #++ }#++ #-- close($new_sock); }
HTH2


In reply to Re: IO::Socket Not responding after period of time and traffic! by Perlbotics
in thread IO::Socket Not responding after period of time and traffic! by aaronwroblewski

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.