Hi all
The situation is as follows.
- I have a device that I connect to via WiFi, this device amongst other things broadcasts a stream of UDP data that I want to read in my App.
- Other Apps on the device (written by other developers) will also want to read these packets.
- I want to be able to receive the broadcast UDP messages without blocking other Apps from doing so.
The problem I have encountered is I seem to be fetching the data in a way that is not playing nice with others.
- If I start the other apps first, they can receive data, and when I connect on my App I am able to receive the UDP messages using NWListener generated connections, or using socket/bind via getaddrinfo or just directly creating a sockaddr_in
- If I start my App and connect first, other Apps are not then able to retrieve any data, I presume because I am binding to the port somehow and hogging it.
I have tried setting SO_REUSEADDR, SO_REUSEPORT on the socket, with no success.
I have not been able to receive any UDP data using recvfrom etc unless I do bind, however from my research I was lead to believe I shouldn't need to do bind when just listening for udp broadcast data.
When using getifaddrs
to view the various connections available to the App, I can see the device as follows.
Name: en0
Is Broadcast (checking ifa_flags & IFF_BROADCAST)
addr: 192.168.4.2
netmask: 255.255.255.0
gateway: 192.168.4.255
If I connect the WiFi device to my Mac, I can use tcdump
to see the udp packets being broadcast as follows.
tcpdump -c 4 -l -n -i en0 'udp and port 4000'
Password:
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on en0, link-type EN10MB (Ethernet), snapshot length 524288 bytes
11:47:27.297327 IP 192.168.4.1.49153 > 192.168.4.3.4000: UDP, length 33
11:47:27.429211 IP 192.168.4.1.49153 > 192.168.4.3.4000: UDP, length 75
11:47:27.911906 IP 192.168.4.1.49153 > 192.168.4.3.4000: UDP, length 52
11:47:28.029487 IP 192.168.4.1.49153 > 192.168.4.3.4000: UDP, length 33
4 packets captured
8 packets received by filter
0 packets dropped by kernel
I feel like I have all the pieces, but I don't understand the correct process for setting up a socket to receive broadcast UDP packets from my device in a way that doesn't affect other Apps running on the device. I am confident to try solutions either using NWListener/NWConnection solutions or BSD Sockets.