@Test
public void test() throws IOException {
OutputStream os = socket.getOutputStream();
ByteArrayOutputStream hs = new ByteArrayOutputStream();
short id = 17;
hs.write(id >> 8);
hs.write((id << 8) >> 8);
boolean query = true;
int opcode = 0; // 0000 0000 0000 0000 0000 0000 0000 0000
int b = 0;
if (query) {// 0
b = b | 0x00000000;
} else {// 1
b = b | 0x00000080;
}
b = b | opcode;
int aa = 0x00000000;
b = b | aa;
int tc = 0x00000000;
b = b | tc;
int rd = 0x00000001;
b = b | rd;
hs.write(b);
b = 0;
int ra = 0;
b = b | ra;
int z = 0x00000000;
b = b | z;
int rcode = 0x00000000;
b = b | rcode;
hs.write(b);
short qdc = 1;
hs.write(qdc >> 8);
hs.write((qdc << 8) >> 8);
short anc = 0;
hs.write(anc >> 8);
hs.write((anc << 8) >> 8);
short nsc = 0;
hs.write(nsc >> 8);
hs.write((nsc << 8) >> 8);
short arc = 0;
hs.write(arc >> 8);
hs.write((arc << 8) >> 8);
ByteArrayOutputStream qs = new ByteArrayOutputStream();
// String domain = "SRI-NIC.ARPA";//SRI-NIC.ARPA. //7SRI-NIC4ARPA0
String domain = "www.hao123.com";//3www6hao1233com0
String[] ss = domain.split("\\.");
for (int i = 0; i < ss.length; i++) {
String label = ss[i];
qs.write(label.length());
qs.write(label.getBytes());
}
qs.write(0);
short qtype = 1;
qs.write(qtype >> 8);
qs.write((qtype << 8) >> 8);
short qclass = 1;
qs.write(qclass >> 8);
qs.write((qclass << 8) >> 8);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
short len = (short) (12 + qs.size());
bos.write(len >> 8);
bos.write((len << 8) >> 8);
bos.write(hs.toByteArray());
// 0, 17, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
bos.write(qs.toByteArray());
// 3, 119, 119, 119, 6, 104, 97, 111, 49, 50, 51, 3, 99, 111, 109, 0, 0, 1, 0, 1
os.write(bos.toByteArray());
os.flush();
InputStream is = socket.getInputStream();
ByteArrayOutputStream rs = new ByteArrayOutputStream();
// 0, 12, 0, 17, -128, 1, 0, 0, 0, 0, 0, 0, 0, 0
// 2, 8, 0, 17, -127, 0, 0, 1, 0, 0, 0, 13, 0, 15, 3, 119, 119, 119, 6, 104, 97, 111, 49, 50, 51, 3, 99, 111, 109, 0, 0, 1, 0, 1, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 20, 1, 104, 12, 103, 116, 108, 100, 45, 115, 101, 114, 118, 101, 114, 115, 3, 110, 101, 116, 0, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 99, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 101, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 98, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 109, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 97, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 103, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 108, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 105, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 102, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 100, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 107, -64, 46, -64, 23, 0, 2, 0, 1, 0, 2, -93, 0, 0, 4, 1, 106, -64, 46, -64, -116, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 5, 6, 30, -64, 108, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 33, 14, 30, -64, 76, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 26, 92, 30, -64, -36, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 31, 80, 30, -64, 92, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 12, 94, 30, -64, -52, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 35, 51, 30, -64, -100, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 42, 93, 30, -64, 44, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 54, 112, 30, -64, -68, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 43, -84, 30, -64, -4, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 48, 79, 30, -64, -20, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 52, -78, 30, -64, -84, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 41, -94, 30, -64, 124, 0, 1, 0, 1, 0, 2, -93, 0, 0, 4, -64, 55, 83, 30, -64, -116, 0, 28, 0, 1, 0, 2, -93, 0, 0, 16, 32, 1, 5, 3, -88, 62, 0, 0, 0, 0, 0, 0, 0, 2, 0, 48, -64, 108, 0, 28, 0, 1, 0, 2, -93, 0, 0, 16, 32, 1, 5, 3, 35, 29, 0, 0, 0, 0, 0, 0, 0, 2, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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