Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

UDP Client Multiple servers

by adwald (Initiate)
on Oct 11, 2012 at 14:56 UTC ( #998468=perlquestion: print w/replies, xml ) Need Help??
adwald has asked for the wisdom of the Perl Monks concerning the following question:

I am needing to perform a leasequery to multiple servers. HI have tried the following code but it keeps locking on the second server. I cannot find anything online as an example. Can someone provide some assistance?

!/usr/bin/perl -w use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; $mac=$ARGV[0]; @dhcp = ("","","","","17 +",""); foreach $server (@dhcp) { print "$server\n"; # create a socket $handle = IO::Socket::INET->new(Proto => 'udp', Broadcast => 1, PeerPort => '67', LocalPort => '67', PeerAddr => $server); # create DHCP Packet $inform = Net::DHCP::Packet->new( op => BOOTREQUEST(), Htype => '1', Hlen => '6', Ciaddr => '0', Chaddr => $mac, Giaddr => $handle->sockhost(), Xid => int(rand(0xFFFFFFFF)), # random xid DHO_DHCP_MESSAGE_TYPE() => DHCPLEASEQUERY ); # send request $handle->send($inform->serialize()) or die "Error sending LeaseQuery +: $!\n"; #receive response $handle->recv($newmsg, 1024) or die; $packet = Net::DHCP::Packet->new($newmsg); $data = $packet->toString(); $handle->close(); } print "$data\n";

Replies are listed 'Best First'.
Re: UDP Client Multiple servers
by BrowserUk (Pope) on Oct 11, 2012 at 15:55 UTC

    What happens if you add use strict; at the top of your code and prefix your variables with my?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      I found the answer to my problem. I needed to eval whether a response was received in an amount of time and escape out if not.

      eval { local $SIG{ALRM} = sub { die 'Timed Out'; }; alarm 1; #receive response $handle->recv($newmsg, 1024) or die; $packet = Net::DHCP::Packet->new($newmsg); $data = $packet->toString(); close $handle; alarm 0; }; close $handle;

        That's not really a good idea. You could just set timeout option on the socket after creating it:

        use Socket qw(SO_RCVTIMEO); use POSIX qw(:errno_h); ...; my $timeout = pack 'L!L!', $sec, $usec; $handle->sockopt(SO_RCVTIMEO, $timeout); ...; my $res = $handle->recv(...); unless(defined $res) { if($! == EAGAIN or $! == EWOULDBLOCK) { say "time out"; } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998468]
Approved by herveus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2017-12-13 09:57 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (355 votes). Check out past polls.