Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Answer to a UDP Broadcast is ignored

by Anonymous Monk
on Apr 29, 2013 at 09:31 UTC ( #1031191=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there,

i'm pretty new to Perl (in fact i started to learn last Friday...), so please forgive me.

There is a server in my local net (in fact it's a SD Card with a WiFi and webserver where you can download the pics the Camera just stored on the Card).

First of all my programm has to find the card on the network. That should be done by sending a UDP Broadcast from a specific to a specific port. The card Answers with a unicast to my PC telling it's IP Addr.

I'm using the following to send the Broadcast:

my $FindCard = IO::Socket::INET->new(Proto=>"udp",LocalPort=>58255,P +eerPort=>55777,PeerAddr=>"192.168.11.255",Broadcast=>1) or die "Can't make UDP socket: $@"; $FindCard->send("");
And use this to get the answer:

$FindCard->recv($datagram,120,$flags);

Running Wireshark i see the Broadcast beeing send, and also the unicastanswer from the server. But my programm happily ignores this answer.

If i change the PeerAddr to the actual IP Address of the server and send the UDP datagram via unicast i get the same answer from the server, but my Programm is able the cope with it.

All i can think of is that in broadcast the PeerAddress i'm sending to and the address answering is different, so it is ignored.

I've tried searching the web for the whole weekend, but all i found where people having similar problems. But none could tell them what to do. So does anybody have a idea what i could search for?

Thanks,
Sven

Comment on Answer to a UDP Broadcast is ignored
Select or Download Code
Re: Answer to a UDP Broadcast is ignored
by VinsWorldcom (Priest) on Apr 29, 2013 at 11:51 UTC

    UDP is connectionless so you probably won't be able to send/receive on a single socket pair (bi-directional). Have you tried opening a listening socket on port 58255 (LocalPort) and having that 'recv' the response as it will be unicast to you? For example

    use strict; use warnings; [...] my $FindCard = IO::Socket::INET->new(Proto=>"udp",LocalPort=>58255,Pee +rPort=>55777,PeerAddr=>"192.168.11.255",Broadcast=>1) or die "Can't make UDP socket: $@"; my $recv = IO::Socket::INET->new(Proto=>"udp",LocalPort=>58255) or die "Can't make UDP socket: $@"; [...] $FindCard->send(""); $recv->recv($datagram,120,$flags); [...]
      Thank you very much! Alltough your code doesn't work this way, you gave me the right hint:
      The way your code works i have a problem that i open the same socket twice. That don't work.
      I tried "ReuseSocket", but it seems my OS (Linux MINT) doesn't support that. So first i opened the Socket, send my Broadcast, closed it an reopened the socket the way you suggested. I'm aware that if the card answers to fast i will have a Problem cause my socket may not be open at that time... i'll have to do some testing.
      But know i receive the Answer and can extract the strings i'm looking for.

      Thanks again!

      Regards,
      Sven

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-07-23 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (154 votes), past polls