Search code examples
javaandroidzeroconfjmdns

JmDNS service not found in ZeroConf browser


I'm developing a web app in Java (with Spark) and I want it to use JmDNS to be able to find this server in my Android app. I was following steps from JmDNS page with service registration, and my code looks like this:

try {
        // Create a JmDNS instance
        JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());

        // Register a service
        ServiceInfo serviceInfo = ServiceInfo.create("_http._tcp.local.", "smart_dom", 4567, "path=index.html");
        jmdns.registerService(serviceInfo);

    } catch (IOException e) {
        System.out.println(e.getMessage());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

I implemented the part about discovering services in my Android app, but it finds nothing. I made some changes according to this question, and even downloaded ZeroConf Browser (it relies on JmDNS), but even this app doesn't see my web app.

My server runs on Windows PC. I know that Windows doesn't support mDNS, but Java should be independant from the platform. What can I be doing wrong?


Solution

  • If anyone was wondering what was wrong, I found the answer.

    In my case the problem was with JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());, especially with InetAddress.getLocalHost() which returned 127.0.0.1 not my LAN adapter wi-fi address. The problem was solved using this solution. It bases on looking for the correct interface via NetworkInterface.getNetworkInterfaces().