Receiving a UDP broadcast

Hello.
I am trying to receive a UDP broadcast on my arduino mega.
i have little knowledge on the topic so if someone could show a code example
for sending and receiving a UDP broadcast and what port should i use it will be very helpful.
thanks. :smiley:

If you mean a UDP packet sent to your Arduino's IP address, then take a look in the ethernet library examples. The UDPSendReceiveString is a pretty good example. It receives a packet, then returns a UDP packet to the sender.

I will just say what i need...
I am making an iPhone app that needs to send information to the arduino.
at first i made the arduino run on a static ip and i communicated with it via telnet and it worked.
but now I want to do it so it doesn't have a static ip so it wont be a problem with other devices.
So, for that i need to send a broadcast massage(correct me if i'm wrong) and listen to it from the arduino and when it comes i will send back my ip and then i know what to do because i did it already.

omer531998:
I will just say what i need...

That is always best.

You may have a bit of trouble with this. The ethernet shield has problems listening on two IP addresses. It does good with multiple ports. I tried to help one user with this with bad results. Here is a link to that thread.
http://forum.arduino.cc/index.php?topic=308706.msg214223

So the only thing i can do is using a static ip? and if thats the case why?!?!?
but really, How can ensure that the ip that i want to use is not taken?

Many models of routers reserve a range of IP addresses to use as static assignments. I always recommend checking the settings in your router to find the dhcp server IP range and use an IP outside that range. One brand of router I use has a dhcp range of 192.168.1.100 to 192.168.1.150. Any 192.168.1.x IP outside that would be good.

You can usually change that dhcp range setting in your router if you wish. I do. :slight_smile:

thank you very much.
you helped me a lot. :smiley:

Hello, i'm the man whose SurferTim is trying to help on the subject,
have a look on my link paperduino - #13 by system - Portugues - Arduino Forum
you can an UDP send packet on broadcast 255.255.255.255 IP and the packet that i receive in return,
i guess same method could be good for you. :wink:

For my probleme, UDP sending for used the UDP remote IP returned and write on it with TCP Client,
i 've no solution at this time instead using 2 arduinos and 2 ethernet shield on a swith but it's not a low cost solution ! :money_mouth_face:

So you can have a look on this following UDP project with UDP Scketh and processing sketch, i test it and it's run fine.
:grin: