in reply to Problems with select() on socket

If your code is more structured, it will be easier for other people to understand your code, and more monks will join to debug for you. More importantly, easier for yourself to debug.

Compare your code with the following rewrite (I am just trying to show a better structure, not doing everything), don't you think the rewrite is much easier to understand and debug?Maybe you can start from here.

use IO::Socket::INET; use IO::Select; use strict; use warnings; my $socket = new IO::Socket::INET(Proto => "udp"); my $select = new IO::Select($socket); my $num_packet_rcvd; my $packet_rcvd; for (4000 .. 4100) {#change this to your logic of going thru peer list my $peer = sockaddr_in($_, inet_aton("localhost")); $socket->send("1234567890", 0, $peer) || die "send: $!";#instead o +f die, your real code shall handle it, for example logging print "packet sent to port $_\n"; } while (1) { my @sockets = $select->can_read(10); last if ($#sockets == -1);#no more reply within a reasonable amoun +t fo time $num_packet_rcvd ++; $socket->recv($packet_rcvd, 100); print "rcvd packet $num_packet_rcvd [$packet_rcvd]\n";#you will re +place this with your stuff }

Replies are listed 'Best First'.
Re: Re: Problems with select() on socket
by Anonymous Monk on Nov 26, 2003 at 14:44 UTC
    Hey, thanks alot for the help. I guess my biggest problem is not truly understanding the way select() works. (yes, I have read the POD, and googled a bit)

    I was under the impression that my original code would switch between sending and recieving as reply packets start coming in.

    In the rewritten code, what will happen if replies start arriving before the "for(..)" loop is finished? will they be handled?

      Yes, the system will queue them for you. Also instead of sending to 3000 at once, you can group them into smaller grounps.