I have a service which has to accept several connections on the same port (and keep them opened).

I was told that by using IO::Select I could accept the incoming connections like this:

  my ( $read, $write, $exceptions ) = IO::Select->select( $pread, $pwrite, $pexceptions );

Here's a piece of code that illustrates a problem I'm having with this:

#!/usr/bin/perl -w use strict; use IO::Socket; use IO::Select; my $sock=new IO::Socket::INET (Localhost => '127.0.0.1', LocalPort => '4000', Proto => 'tcp', Listen => 5, ReuseAddr => 1, ); die "Socket could not be created. Reason $!" unless $sock; my $pread = IO::Select->new; my $pwrite = IO::Select->new; my $pexceptions = IO::Select->new; $pexceptions->add($sock); while (1) { print "1\n"; my ( $read, $write, $exceptions ) = IO::Select->select( $pread, $pwr +ite, $pexceptions ); print "2\n"; } close($sock);

My understandment from what was explained to me is that the while cycle would go on and on, and that I'd have the original socket in $exceptions when a new connection was arriving; then I'd be able to open the connection and put it in $pread.

At each cycle I'd be able to get all the requests from $read.

The problem is, nothing's happening! And when I say "nothing", I really mean "nothing". The "2\n" is not printed, only the first "1\n".

I have tried a timeout on the select, but in that case I get the while to move on but $exceptions is always empty, even when a client is trying to connect.

What am I doing wrong?

(for the record, my specific problem is that I need a service that needs to answer to many clients, and all those clients have many requests, permanently, so I'd like for them to open only one connection and keep using it, instead of opening a new one at each request; I really need to avoid that)

(also for the record, forking is not a possibility, as there's information that has to be shared)

TIA.


In reply to IO::Select and sockets by cog

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



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