in reply to IO::Select and sockets

The docs say that the return from the select() method is three array refs, so I don't know why you create 3 IO::Select objects as the arguments. If you are looking for sockets to read from, then you should probably use the can_read() method. The code should be more like:
my $select = IO::Select->new(); my $sock = IO::Socket->new(...); $select->add($sock); while (my @sockets = $select->can_read()) { ... }
Update: I'll also throw in a plug for Lincoln Stein's "Network Programming with Perl" book.

Replies are listed 'Best First'.
Re^2: IO::Select and sockets
by cog (Parson) on Dec 29, 2005 at 18:22 UTC
    OK, that code doesn't block.

    Trying...

    Hey, I managed to make it work! :-D

    I had tried the can_read before, but only now did it work (probably I was doing something differently than what you suggested).

    Thanks, runrig.

    Since I had a lot of trouble finding this information, I'll put here the script I have working at the moment so that others may solve this problem faster in the future:

    #!/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 $select = IO::Select->new(); $select->add($sock); while (my @sockets = $select->can_read()) { for my $incoming (@sockets) { if ($incoming == $sock) { my $new_sock = $incoming->accept; $select->add($new_sock); } else { print "new guy calling: $incoming\n"; } } } close($sock);
Re^2: IO::Select and sockets
by cog (Parson) on Dec 29, 2005 at 18:09 UTC
    The docs say that the return from the select() method is three array refs

    From the docs I'm reading:

      select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )

    READ, WRITE and EXCEPTION are either undef or IO::Select objects

      Ooops. My mistake. I was reading the next paragraph about the return values and confusing it with the first paragraph about the arguments (update: need coffee). I never have used that method with IO::Select (I see documentation but I don't see it used in Stein's book either)...I usually just use can_read() (which is also what is mostly used in the book).
Re^2: IO::Select and sockets
by ikegami (Patriarch) on Dec 29, 2005 at 18:29 UTC
    I have found can_read to be rather useless in practice. can_read allows one to read from sockets without blocking, but one usually needs to also write to sockets without blocking. The select method does that.
      I think it depends whether you're connecting to a service, or creating a service and waiting for connections to it. If I need to do both at the same time, then I'd probably use select(), otherwise, I'll use a can_* method (or maybe I'm missing something and doing it inherently wrong?).

        Any servers that handles mutliple connections must

        • Wait to read incomming connections from the server socket.
        • Wait to read incomming requests from each client socket from which a request hasn't been obtained.
        • Wait to write reponses to each client socket from which a request has been obtained.
        • Wait for errors.

        All of these can occur at the same time, which is why can_* are insufficient, and select is desired on the server side.

         

        Any clients that handles mutliple connections must

        • Wait to write requests to every server socket to which a request has not been sent.
        • Wait to read incomming responses from each server socket to which a request has been sent.
        • Wait for errors.

        All of these can occur at the same time, which is why can_* are insufficient, and select is desired on the client side.