I must be missing something very basic here, but I'm having a problem with connecting to a single threaded server based on IO::Socket.
Basically, the following does not block until the other side does a "$server->accept".
my $sock = new IO::Socket::INET( PeerAddr => '127.0.0.1',
PeerPort => 12000,
Proto => 'tcp',
Blocking => 1);
print $sock "Hello\n";
I had expected that if the server was written as such
my $server = IO::Socket::INET->new( LocalPort => 12000,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1
);
die $! unless $server;
while (my $sock = $server->accept) {
print "Connected to sock\n";
print while <$sock>;
}
Once it had accepted one connection, any subsequent ones would block until it could accept() again.
I am obviously wrong here.
The above is contrived, of course, but what I want is the part that connects to block until the server is finished with it's current request.
As I said, I'm missing something obvious I'm sure, so please help out a brain-addled fellow monk.
EDIT
Here is the test code I wrote in it's entirety:
use strict;
use warnings;
use IO::Socket::INET;
if ($ARGV[0] == 1) {
my $server = IO::Socket::INET->new( LocalPort => 12000,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1
);
die $! unless $server;
while (my $sock = $server->accept) {
print "Connected to sock\n";
print while <$sock>;
}
}
if ($ARGV[0] == 2) {
my $sock = new IO::Socket::INET( PeerAddr => '127.0.0.1',
PeerPort => 12000,
Proto => 'tcp',
Blocking => 1
);
print "sock is $sock\n";
}
Then what I did was this:
$ perl SocketT.pl 1 (to launch as a server)
$ telnet localhost 12000 (in another window, to tie up the server)
$ perl SocketT.pl 2 (to test if it would connect and print out)
-
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.