0

I am implementing an java program that scans some of the smart devices that I have, using SSDP discovery. The code that I am using detects only 2 devices among 10 that are connected to my local network. I could not understand the reason is.

My question is, why I am not able to get all the devices that are in my local network.

This is the code that I am using

static final String HOST = "Host:" + SSDP.ADDRESS + ":" + SSDP.PORT;
static final String MAN = "Man: \"ssdp:discover\"";
 static final String NEWLINE = "\r\n";

int mMX = 10;    /* seconds to delay response */
String mST;     /* Search target */

public SSDPSearchMsg(String ST) {
    mST = ST;
}

public int getmMX() {
    return mMX;
}

public void setmMX(int mMX) {
    this.mMX = mMX;
}

public String getmST() {
    return mST;
}

public void setmST(String mST) {
    this.mST = mST;
}

@Override
public String toString() {
    StringBuilder content = new StringBuilder();

    content.append(SSDP.SL_MSEARCH).append(NEWLINE);
    content.append(HOST).append(NEWLINE);
    content.append(MAN).append(NEWLINE);
    content.append(mST).append(NEWLINE);
    content.append("MX:" + mMX).append(NEWLINE);
    content.append(NEWLINE);

    return content.toString();
}

Below is few constants

 /* New line definition */
public static final String NEWLINE = "\r\n";

public static final String ADDRESS = "239.255.255.250";
public static final int PORT = 1900;

/* Definitions of start line */
public static final String SL_NOTIFY = "NOTIFY * HTTP/1.1";
public static final String SL_MSEARCH = "M-SEARCH * HTTP/1.1";
public static final String SL_OK = "HTTP/1.1 200 OK";

/* Definitions of search targets */
public static final String ST_RootDevice = "ST: upnp:rootdevice";
public static final String ST_ContentDirectory = "ST: urn:schemas-upnp-org:service:ContentDirectory:1";
public static final String ST_ALL="ST: ssdp:all";
public static final String ST_Media="ST:urn:schemas-upnp-org:device:MediaRenderer:1";


/* Definitions of notification sub type */
public static final String NTS_ALIVE = "NTS:ssdp:alive";
public static final String NTS_BYE = "NTS:ssdp:byebye";
public static final String NTS_UPDATE = "NTS:ssdp:update";

Main class

public class SSDPSocket {
SocketAddress mSSDPMulticastGroup;
MulticastSocket mSSDPSocket;

public SSDPSocket() throws IOException {
    InetAddress localInAddress = InetAddress.getLocalHost();
    System.out.println("Local address: " + localInAddress.getHostAddress());

    mSSDPMulticastGroup = new InetSocketAddress(SSDP.ADDRESS, SSDP.PORT);
    mSSDPSocket = new MulticastSocket(new InetSocketAddress(localInAddress,
            0));

    NetworkInterface netIf = NetworkInterface
            .getByInetAddress(localInAddress);
    mSSDPSocket.joinGroup(mSSDPMulticastGroup, netIf);
}

/* Used to send SSDP packet */
public void send(String data) throws IOException {
    DatagramPacket dp = new DatagramPacket(data.getBytes(), data.length(),
            mSSDPMulticastGroup);

    mSSDPSocket.send(dp);
}

/* Used to receive SSDP packet */
public DatagramPacket receive() throws IOException {
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, buf.length);

    mSSDPSocket.receive(dp);

    return dp;
}

public void close() {
    if (mSSDPSocket != null) {
        mSSDPSocket.close();
    }
}

/* For test purpose */
public static void main(String[] args) {

    SSDPSearchMsg search = new SSDPSearchMsg(SSDP.ST_Media);
    System.out.println(search.toString());

    SSDPSocket sock;
    try {
        sock = new SSDPSocket();
        sock.send(search.toString());

        while (true) {
            DatagramPacket dp = sock.receive();
            String c = new String(dp.getData());
            System.out.println(c);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    }
}

Can anyone please suggest a solution for this problem?

チーズパン
  • 2,752
  • 8
  • 42
  • 63
Arun Kumar
  • 1
  • 1
  • 1
  • If you have another SSDP client that gets 10 replies and your client that gets 2 replies, it should be easy to get a network traffic dump and see what is different in the two cases (see e.g. Wireshark). – Jussi Kukkonen Dec 11 '15 at 09:06
  • Also, you are searching for MediaRenderer devices: are you sure there are 10 mediarenderers in your network? – Jussi Kukkonen Dec 11 '15 at 09:12
  • I even tried using ssdp:all, still I will get only few not all devices. – Arun Kumar Dec 14 '15 at 04:30
  • When I run gssdp command in Linux system I am able to get all the devices that are connected to my network. It detects windows system, Linux, Ubuntu and smart devices like Philips hue, Belkin wemo. – Arun Kumar Dec 14 '15 at 04:34

1 Answers1

0

I just worked on an SSDP Java client and studied the spec carefully.

You are missing an "MX" header. If the header MX is not set, the SSDP Service "SHOULD" not respond to your request. Therefore, try adding an "MX: 1" header and see where this goes.

The Schneider database was of good help for this issue.

Also, you can take a look at the ssdp client if you still are trying to use it.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38