@Test public void testResolvePacket() throws IOException { byte[] actual = queryPacket("device-1234._example._tcp.local", QCLASS_INTERNET | CLASS_FLAG_UNICAST, QTYPE_TXT, QTYPE_SRV); byte[] expected = new ByteBuilder() .hex("00 00 00 00 00 02 00 00 00 00 00 00") .hex("0b").ascii("device-1234") .hex("08").ascii("_example") .hex("04").ascii("_tcp") .hex("05").ascii("local") .hex("00 00 10 80 01 c0 0c 00 21 80 01") .build(); assertArrayEquals(expected, actual); }
static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(0); dos.writeShort(qtypes.length); dos.writeShort(0); dos.writeShort(0); dos.writeShort(0); int fqdnPtr = -1; for (int qtype : qtypes) { if (fqdnPtr == -1) { fqdnPtr = dos.size(); writeFQDN(serviceName, dos); } else { dos.write(0xc0 | (fqdnPtr >> 8)); dos.write(fqdnPtr & 0xFF); } dos.writeShort(qtype); dos.writeShort(qclass); } dos.close(); return bos.toByteArray(); }
MDNSDiscover { static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(0); dos.writeShort(qtypes.length); dos.writeShort(0); dos.writeShort(0); dos.writeShort(0); int fqdnPtr = -1; for (int qtype : qtypes) { if (fqdnPtr == -1) { fqdnPtr = dos.size(); writeFQDN(serviceName, dos); } else { dos.write(0xc0 | (fqdnPtr >> 8)); dos.write(fqdnPtr & 0xFF); } dos.writeShort(qtype); dos.writeShort(qclass); } dos.close(); return bos.toByteArray(); } }
MDNSDiscover { static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(0); dos.writeShort(qtypes.length); dos.writeShort(0); dos.writeShort(0); dos.writeShort(0); int fqdnPtr = -1; for (int qtype : qtypes) { if (fqdnPtr == -1) { fqdnPtr = dos.size(); writeFQDN(serviceName, dos); } else { dos.write(0xc0 | (fqdnPtr >> 8)); dos.write(fqdnPtr & 0xFF); } dos.writeShort(qtype); dos.writeShort(qclass); } dos.close(); return bos.toByteArray(); }  }
MDNSDiscover { static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(0); dos.writeShort(qtypes.length); dos.writeShort(0); dos.writeShort(0); dos.writeShort(0); int fqdnPtr = -1; for (int qtype : qtypes) { if (fqdnPtr == -1) { fqdnPtr = dos.size(); writeFQDN(serviceName, dos); } else { dos.write(0xc0 | (fqdnPtr >> 8)); dos.write(fqdnPtr & 0xFF); } dos.writeShort(qtype); dos.writeShort(qclass); } dos.close(); return bos.toByteArray(); }  static void discover(String serviceType, Callback callback, int timeout); static Result resolve(String serviceName, int timeout); }
MDNSDiscover { static byte[] queryPacket(String serviceName, int qclass, int... qtypes) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); dos.writeInt(0); dos.writeShort(qtypes.length); dos.writeShort(0); dos.writeShort(0); dos.writeShort(0); int fqdnPtr = -1; for (int qtype : qtypes) { if (fqdnPtr == -1) { fqdnPtr = dos.size(); writeFQDN(serviceName, dos); } else { dos.write(0xc0 | (fqdnPtr >> 8)); dos.write(fqdnPtr & 0xFF); } dos.writeShort(qtype); dos.writeShort(qclass); } dos.close(); return bos.toByteArray(); }  static void discover(String serviceType, Callback callback, int timeout); static Result resolve(String serviceName, int timeout);  }