Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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=>"",Broadcast=>1) or die "Can't make UDP socket: $@"; $FindCard->send("");
And use this to get the answer:


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?


Comment on Answer to a UDP Broadcast is ignored
Select or Download Code
Replies are listed 'Best First'.
Re: Answer to a UDP Broadcast is ignored
by VinsWorldcom (Vicar) 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=>"",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!


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2015-10-09 20:25 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (251 votes), past polls