http://qs1969.pair.com?node_id=771242

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
#!/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 ); }
writer.pl
#!/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++; }
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).

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?