in reply to IO::Socket Not responding after period of time and traffic!

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

Replies are listed 'Best First'.
Re^2: IO::Socket Not responding after period of time and traffic!
by aaronwroblewski (Initiate) on Aug 18, 2010 at 10:38 UTC
    Thanks,

    It is running alot more stable for the one connection now.

    However there is a small problem and I think it is because of the location of you sub REAP line as you suggest in your code, that seems to not allow any further connections while the child previous is still open.

    If i do a lsof after the first connection is made and still active, it shows two Listens and one Established.

    The code then gets stuck at REAP and accepts no further connections.

    Any how, it is late and I am getting square eyes, I will look at it further tomorrow after some more meditation..

    Thanks Again.

Re^2: IO::Socket Not responding after period of time and traffic!
by aaronwroblewski (Initiate) on Aug 16, 2010 at 09:22 UTC

    Thanks, Ill look into some of your suggestions, I will have to read up on alot of them.

    Thanks again.

Re^2: IO::Socket Not responding after period of time and traffic!
by aaronwroblewski (Initiate) on Aug 17, 2010 at 09:12 UTC

    I have looked at quite a few of the suggestions made throughout this thread and made some changes without much improvement.

    I run my script using the suggested Devel:Trace and found that the while(1) loop that you pointed out was indeed causing some issues with resource (Race Condition).

    So I did what you suggested and removed the Parent Loop and the close $sock;.

    now my problem is that after a connection is made, passed off to the child and child exits, the Parent/script exits.

    How can I make the Parent sit idle waiting for the next connection without re creating the socket?.