Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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 = ("10.16.30.4","10.16.30.5","172.31.45.248","172.31.45.249","17 +2.31.35.248","172.31.35.249"); 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";

Comment on UDP Client Multiple servers
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998468]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (12)
As of 2014-09-19 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls