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?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.