javaiosiphonejmdnsrobovm

UnknownHostException from getLocalHost on iOS device in RoboVM


I am trying to initialize JmDNS on an iOS device, getting the address with the line:

InetAddress adr = InetAddress.getLocalHost();

It works fine in the iphone simulator, however when tested on a physical iOS device this causes an UnknownHostException, with the stacktrace:

java.net.UnknownHostException: Unable to resolve host "#{name of iphone}": nodename nor servname provided, or not known
at libcore.io.GaiException.rethrowAsUnknownHostException(GaiException.java)
at java.net.InetAddress.lookupHostByName(InetAddress.java)
at java.net.InetAddress.getLocalHost(InetAddress.java)
at com.glassknuckle.ThiefTown.Networking.ThiefClient$NETWORK_TASK.run(ThiefClient.java)
at java.lang.Thread.run(Thread.java)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NONAME (nodename nor servname provided, or not known)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java)
... 4 more

I've read advice saying to pass in "127.0.0.1" as the address, but this doesn't work when other devices are trying to connect to it via that IP. I'm not sure what to do from here- I definitely need to get the local IP address but I don't know how else to get it. This appears to be related to roboVM but I am not fully sure. Perhaps someone can shed some light on it?


Solution

  • Figured this out myself. Apparently iOS blocks you from getting the local IP address via getLocalHost. I had to use RoboVM bridges to insert an Objective-C script to do it: http://blog.robovm.org/2013/08/support-for-objective-c-blocks.html