Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a small app that queries a large (~3000) list of UDP servers, and parses thier results to be displayed in a tk window. On my local machine (Linux) it works just fine. Several other people who have tried it, however, cannot seem to get it to work. That is, it sends out the packets, but receives and parses only a few replys. I can't seem to figure it out. The general idea of what I am doing...
sub CheckServers { # go down the list of servers # and sent a packet to each # one to request info my $socket = IO::Socket::INET->new( Proto => 'udp', Type => SOCK_DGRAM); my $select = IO::Select->new($socket); my %responses; CHECK_SERVERS: while (@servers){ my $toread = $select; my $towrite = @servers ? $select : undef; my ($readref, $writeref) = IO::Select->select( $toread,$towrite,undef,$timeout ); last unless ($readref || $writeref); foreach my $sock ( @{ $writeref } ) { $sent++; my $server = shift @servers; my ($ip, $port) = split (/.:/, $server); next unless $port && $ip; my $paddr = sockaddr_in($port, inet_aton($ip)); my $ret = $sock->send($HOST_QUERY, 0, $paddr) || die "send: $!"; $waiting{$ip} = Time::HiRes::time; } foreach my $sock ( @{ $readref } ) { $rcvd++; my $rc = $sock->recv($reply, 10_000); next unless $rc; my ($port, $addr) = sockaddr_in($rc); my $ip = inet_ntoa($addr); if ( $reply ) { ... do stuff ... } next CHECK_SERVERS; } } }

Replies are listed 'Best First'.
Re: Problems with select() on socket
by pg (Canon) on Nov 26, 2003 at 05:17 UTC

    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 }
      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.