Our game needs a multiplayer feature over wi-fi, in which two devices find each other in order to play. I’ve been experimenting with .Net UDP broadcasting for this part: the devices advertice themselves by broadcasting in the network.
This works fine as long as all devices are connected to only the one network used for this. However, when testing this with my Mac, which was also connected to an Ethernet, all the broadcasts went into the Ethernet (instead of the wi-fi) so the iPhone never saw them.
Now I figured I’d need to find the specific broadcasting address of the wi-fi subnet. In .Net 2.0 there is the NetworkInformation namespace with handy classes that can be used to access the different networks (called “network interfaces” in the API). But alas, we don’t get to use .Net 2.0 in Unity iPhone…
Does anyone know how to access the different networks your device is connected to, and somehow choose which one to broadcast into? Thanks in advance for any help.
Perhaps it’s so… Sounds inconvenient, for such a simple operation…
The only thing I’d need to know, really, would be the different IP addresses assigned to my device by the various networks. Then I could guess at the broadcast addresses and send UDP datagrams to them all. Does anyone know how to do that? DNS (sounds like something you can’t be sure is available…)?
var hostName : String = Dns.GetHostName();
var ipAddresses : IPAddress[] = Dns.GetHostByName(hostName).AddressList;
Then I iterate over them and combine them with various subnet masks to get all the possible subnet addresses. With most network configurations, this sends into most networks as expected. However, if I have both Ethernet and an ad hoc wifi on the computer, and the Ethernet is higher in the priority list, no packets are sent to the wifi subnet. (If Airport has higher priority, then packets are sent to both networks.)
Does the Bonjour deal with this issue any better? Are there any other solutions?