carlin has asked for the wisdom of the Perl Monks concerning the following question:
I was writing a simple pair of scripts to test out IO::Socket; reader.pl which opens the port, listens and prints out whatever it's fed and writer.pl which writes to the socket. For fun I turned writer.pl into an infinite loop, however after a while it fails to connect.
reader.pl
When it happens, writer.pl can eventually be restarted (it instantly dies with "Could not connect" a few times though) while leaving reader.pl running. That suggests that reader.pl stops responding but even after killing reader.pl and restarting both scripts, writer.pl instantly dies a couple of times.
Why is this happening?
reader.pl
writer.pl#!/usr/bin/perl use strict; use warnings; use IO::Socket; my $socket = new IO::Socket::INET( LocalHost => '127.0.0.1', LocalPort => 7070, Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket\n" unless $socket; while( 1 ) { my $input = $socket->accept(); print <$input> ."\n"; close( $input ); }
Removing the print statement in reader.pl fixes this, but replacing print with sleep(1) doesn't make it occur (I thought the slight delay in making it print must have been blocking).#!/usr/bin/perl use strict; use warnings; use IO::Socket; sub makeSock { my $socket = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => 7070, Proto => 'tcp', ); die "Could not connect\n" unless $socket; return $socket; } my $i = 1; while( 1 ) { my $socket = makeSock(); print $socket $i; close( $socket ); $i++; }
When it happens, writer.pl can eventually be restarted (it instantly dies with "Could not connect" a few times though) while leaving reader.pl running. That suggests that reader.pl stops responding but even after killing reader.pl and restarting both scripts, writer.pl instantly dies a couple of times.
Why is this happening?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: IO::Socket disconnects in loop
by ikegami (Patriarch) on Jun 13, 2009 at 19:48 UTC | |
by carlin (Beadle) on Jun 14, 2009 at 04:06 UTC | |
by ikegami (Patriarch) on Jun 14, 2009 at 07:57 UTC | |
Re: IO::Socket disconnects in loop
by Perlbotics (Archbishop) on Jun 13, 2009 at 19:52 UTC | |
by carlin (Beadle) on Jun 14, 2009 at 04:13 UTC |
Back to
Seekers of Perl Wisdom