2015-12-11 3 views
0

Je suis en train d'implémenter un programme Java qui analyse certains de mes périphériques intelligents grâce à la découverte SSDP. Le code que j'utilise ne détecte que 2 appareils parmi 10 connectés à mon réseau local. Je ne pouvais pas comprendre la raison est.Android SSDP Service Discovery

Ma question est, pourquoi je ne suis pas en mesure d'obtenir tous les périphériques qui sont dans mon réseau local.

Voici le code que j'utilise

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(); 
} 

Ci-dessous est quelques constantes

/* 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"; 

Classe principale

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(); 
     } 
    } 
} 

Quelqu'un peut-il s'il vous plaît proposer une solution à ce problème?

+0

Si vous avez un autre client SSDP qui reçoit 10 réponses et votre client qui reçoit 2 réponses, il devrait être facile d'obtenir un vidage de trafic réseau et voir ce qui est différent dans les deux cas (voir par exemple Wireshark). – jku

+0

En outre, vous êtes à la recherche d'appareils MediaRenderer: êtes-vous sûr qu'il y a 10 médias sur votre réseau? – jku

+0

J'ai même essayé d'utiliser ssdp: tout, encore je vais obtenir seulement quelques périphériques pas tous. –

Répondre

0

Je viens de travailler sur un client Java SSDP et j'ai étudié la spécification avec soin.

Il vous manque un en-tête "MX". Si l'en-tête MX n'est pas défini, le service SSDP "DEVRAIT" ne répond pas à votre demande. Par conséquent, essayez d'ajouter un "MX: 1" en-tête et voir où cela se passe. Le Schneider database a été d'une aide précieuse pour ce problème.

En outre, vous pouvez jeter un oeil à la ssdp client si vous essayez toujours de l'utiliser.