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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.