cog has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: IO::Select and sockets
by ikegami (Patriarch) on Dec 29, 2005 at 18:26 UTC | |
|
Re: IO::Select and sockets
by runrig (Abbot) on Dec 29, 2005 at 18:04 UTC | |
by cog (Parson) on Dec 29, 2005 at 18:22 UTC | |
by ikegami (Patriarch) on Dec 29, 2005 at 18:29 UTC | |
by runrig (Abbot) on Dec 29, 2005 at 18:36 UTC | |
by ikegami (Patriarch) on Dec 29, 2005 at 20:40 UTC | |
by cog (Parson) on Dec 29, 2005 at 18:09 UTC | |
by runrig (Abbot) on Dec 29, 2005 at 18:23 UTC |