Class Discovery

java.lang.Object
com.codename1.backend.Discovery

public final class Discovery extends Object

Finds and is found on the local network over multicast DNS (Bonjour, DNS-SD): the same records the client's BonjourBrowser and BonjourPublisher read and write, so a phone app discovers a device's backend with no address configured, and a device finds a gateway the same way.

Discovery.Advertisement ad = Discovery.advertise("Hall Thermostat", "_thermo._tcp", 8080,
        Collections.singletonMap("api", "/v1"));
Discovery.browse("_gateway._tcp", new Discovery.Listener() {
    public void found(Discovery.Service s) { connect(s.getHost(), s.getPort()); }
    public void lost(Discovery.Service s) { }
});

The protocol runs on one daemon thread, and listeners are called on it: a server has no event dispatch thread, so a listener that does real work hands it to a task. The thread exits when nothing is advertised or browsed. A name another host already uses gets a numeric suffix, as Bonjour does; Discovery.Advertisement.getName() says which name was taken. IPv4 only.