Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Greetings monks;
I'm trying to get a non-forking perl TCP server (at its simplest) to listen to multiple local ports bound to the same address and am having a time making it work. What I'm doing appears logical... but just doesn't want to do its thing.
I use IO::Select to watch the first port, and figured it'd be as simple as add()ing another server handle to the Select handle's list... apparently not.
The below code produces a working server on port 5000, however when I connect to port 5050 the script terminates with a 'Broken pipe' error, closing the telnet connection rather impolitely.
my $server = IO::Socket::INET->new(LocalPort => 5000, Listen => 10, Reuse => 1) or die "Can't make server socket: $@\n"; my $serverB = IO::Socket::INET->new(LocalPort => 5050, Listen => 10, Reuse => 1) or die "Can't make server socketB: $@\n"; my $select = IO::Select->new(($server, $serverB)); while (1) { foreach my $client ($select->can_read(1)) { if ($client == $server) { $client = $server->accept(); $select->add($client); print $client "Howdy\n"; } else { print $client "G'bye\n"; $select->remove($client); close($client); } } }
If I switch the Select->new statement, I get the same results, so I know its not in the way I add the handle to the Select.
my $select = IO::Select->new(); $select->add($server); $select->add($serverB);
What fundamental fact am I missing here that prevents this from working like I want it to? (Sounds suspiciously like "Do what I want, not what I say")
Thanks all;

-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

In reply to Listening on multiple ports using IO::Select by JPaul

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?

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-12-02 18:04 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (18 votes). Check out past polls.