3

On an iPhone I have created a UDP socket and bound it to the WLAN interface. I enabled SO_BROADCAST and I am able to send broadcast packets to the network (confirmed by a client running on my notebook.) But the UDP socket on the iPhone is unable to receive broadcast packets.

In my opinion this is a kernel bug, because not allowing a socket with SO_BROADCAST enabled to receive any broadcast packet, is a bad design decision.

The problem is further described here and here. The most popular solution to this problem is to bind to INADDR_ANY. However, it would be nice to be able to bind an UDP socket to only one interface while still receiving broadcast packets! Is there really no way to achieve this?

(When I bind a socket to 192.168.1.7 on a class C net (/24), it should accept packets sent to 192.168.1.7 and packets sent to 192.168.1.255!)

Thanks!

Community
  • 1
  • 1
Michael
  • 6,451
  • 5
  • 31
  • 53

1 Answers1

2

pls check below link may be it's helpful.

Receive udp broadcast packets ios

iPhone UDP broadcast and response

iOS UDP broadcast vs. PHP UDP broadcast

https://github.com/robbiehanson/CocoaAsyncSocket

Community
  • 1
  • 1
Renish Dadhaniya
  • 10,642
  • 2
  • 31
  • 56
  • Dude, you just answered the much easier question "How to receive UDP broadcast packets on INADDR_ANY interface?" I know how to do that already. Btw, I already use CocoaAsyncSocket. – Michael Jan 14 '14 at 11:34
  • http://stackoverflow.com/questions/3571038/how-to-identify-senders-ip-address-of-a-udp-packet-received-on-ios-device – Renish Dadhaniya Jan 14 '14 at 11:40
  • you still don't understand. in the link you provided, the socket is bound to INADDR_ANY! – Michael Jan 14 '14 at 11:51
  • @Michael have you found any workaround for this one? – Valerii Hiora Feb 21 '15 at 08:14
  • @ValeriiHiora: unfortunately not. i had another idea lately, but I haven't tested it yet, because I don't need to solve this problem any longer: maybe you can bind the socket to the local broadcast address? So, create 2 sockets: one bound to "192.168.0.4" and the other bound to "192.168.0.255" and receive messages on both. (to get the broadcast addr you need `getifaddrs` and then `bc_addr=ip_addr | ( ~netmask)` ). use at your own risk... – Michael Feb 21 '15 at 19:51