0
votes

I have a web service and on startup I wanted to announce the service with a UPnP compatible NOTIFY to the local network. If possible I won't include a lib (like cling) and make it simple as possible.

The code so far:

String NOTIFY
            = "NOTIFY * HTTP/1.1\r\n"
            + "NTS:ssdp:alive\r\n"
            + "Location: http://192.168.1.10\r\n\r\n";

    InetAddress addr;
    MulticastSocket socket;
    DatagramPacket dp;

    try {
        addr = InetAddress.getByName("239.255.255.250");
        socket = new MulticastSocket(1900);
        socket.setReuseAddress(true);
        socket.setSoTimeout(3000);
        socket.joinGroup(addr);

        byte[] buf = NOTIFY.getBytes("UTF-8");
        dp = new DatagramPacket(buf, buf.length, addr, 1900);

        // send out 10 notifys, then stop to send
        for (int i = 10; i > 0; i--) {
            socket.send(dp);
            Thread.sleep(3000);
        }
    } catch (Exception ex) {
        System.err.println(ex);
    }

I'm checking back with the Clink workbench, but I get nothing listed in the UI. Propably the message is not appropiated formated ?

1

1 Answers

1
votes

You need to include additional fields in your announcement - NT and USN

You need to send 3+num_services announcements with different values for the NT header:

  • upnp:rootdevice
  • uuid
  • device type
  • service type (one per service your device offers)

The USN header also varies with these. See the device architecture doc, 1.2.2 Device available - NOTIFY with ssdp:alive in the docs bundle from the UPnP forum for details.