Hello

Hello, 

Is there any special consideration for UDP broacasting under Android and OSX ?

I've made a crossplatform socket component to broadcast a message and let different devices knows of each others.

what I don't understand is that Windows receives all the broadcast when Android and OSX don't. The 3 devices are on the same Wifi router.

the socket is setup this way:

so := socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
opt := 1;
setsockopt(so, SOL_SOCKET, SO_REUSEADDR,@opt, SizeOf(opt));
setsockopt(so, SOL_SOCKET, SO_BROADCAST,@opt, SizeOf(opt));
addr.sin_family := AF_INET;
addr.sin_port := htons(2048);
addr.sin_addr := INADDR_ANY;
bind(so, addr, sizeof(addr));

Sending is done this way:
addr.sin_family := AF_INET;
addr.sin_port := htons(2048);
addr.sin_addr := INADDR_BROADCAST;
sendto(so, data, data_len, addr, addr_len);

I've tried to create 2 sockets, one for sending, and the other for reading. the sending socket was bind with
 addr.sin_port := 0;
 addr.sin_addr := my_local_address;
but it changes nothing

I've also tried to replace INADDR_BROADCAST by the network broadcast address...still don't get anything.

Any clue ?

Comments

  1. of course the bug seems to be in the unspoken part :D

    I use select(), the first parameter is ignored by Windows, but should be set to the highest socket handle, not the handles count.

    ReplyDelete
  2. While we're at it, do you clear sin_zero? Heard it could cause issues on some platforms if you don't.

    ReplyDelete
  3. yes I do...the MACOS pb was the nfds parameter of select() function...but under Android recvfrom() called directly without select() never returns...

    ReplyDelete
  4. Paul TOTH The joys of cross-platform programming :)

    ReplyDelete
  5. GOT IT !

    ok, they are 2 special things under Android

    1) you have to call WifiManager.createMulticastLock (with android.permission.CHANGE_WIFI_MULTICAST_STATE) to enable your application to receive UDP broadcast, not especially for multicast but also for broadcast messages.

    2) do not bind your socket to the device IP, keep INADDR_ANY with desired port or you'll get nothing.

    ReplyDelete

Post a Comment