Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)

mr.nick ...


In reply to IO::Socket connect fails to block by mr.nick

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-03-29 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found