mr.nick has asked for the wisdom of the Perl Monks concerning the following question:
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".
I had expected that if the server was written as suchmy $sock = new IO::Socket::INET( PeerAddr => '127.0.0.1', PeerPort => 12000, Proto => 'tcp', Blocking => 1); print $sock "Hello\n";
Once it had accepted one connection, any subsequent ones would block until it could accept() again.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>; }
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:
Then what I did was this: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"; }
$ 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)
mr.nick ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: IO::Socket connect fails to block
by zwon (Abbot) on Jan 07, 2012 at 03:46 UTC | |
by cavac (Parson) on Jan 07, 2012 at 13:12 UTC | |
Re: IO::Socket connect fails to block
by mr.nick (Chaplain) on Jan 06, 2012 at 19:56 UTC | |
Re: IO::Socket connect fails to block
by gman (Friar) on Jan 06, 2012 at 19:21 UTC | |
by mr.nick (Chaplain) on Jan 06, 2012 at 19:42 UTC | |
by mr.nick (Chaplain) on Jan 18, 2012 at 13:42 UTC |
Back to
Seekers of Perl Wisdom