我在bep_0005页面阅读了DHT协议。
但是当我发送ping查询或find_node查询时,服务器响应一个乱码文本( router.bittorrent.com:6881 或 dht.transmissionbt.com:6881 < / em>的)
以下是Java源代码
public String ping(final String id) {
System.out.println("Start ping:" + id);
Bencode bencode = new Bencode();
byte[] encoded = bencode.encode(new HashMap<Object, Object>() {
private static final long serialVersionUID = 4225164001818744013L;
{
put("t", "tr");
put("y", "q");
put("q", "ping");
put("a", new HashMap<Object, Object>() {
private static final long serialVersionUID = -6092073963971093460L;
{
put("id", id);
}
});
}
});
byte[] result = client.send(new String(encoded, bencode.getCharset()));
Map<String, Object> dict = bencode.decode(result, Type.DICTIONARY);
System.out.println("Bdecoded Data:" + dict);
return "";
}
发送数据包
ping Query = {“t”:“aa”,“y”:“q”,“q”:“ping”,“a”:{“id”:“abcdefghij0123456789”}}
bencoded = d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe
遵循bep_0005协议,响应如下:
Response = {“t”:“aa”,“y”:“r”,“r”:{“id”:“mnopqrstuvwxyz123456”}}
bencoded = d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re
但我的回答是:
回应= {ip = P ,r = {id =2 NisQ J ) F| g},t = tr,y = r}
bencoded = d2:ip6: P 1:rd2:id20:2 NisQ J ) F| ge1:t2:tr1:y1:re
发送udp部分Java代码是:
public byte[] send(String sendData) {
DatagramSocket client;
try {
client = new DatagramSocket();
client.setSoTimeout(5000);
byte[] sendBuffer;
sendBuffer = sendData.getBytes();
InetAddress addr = InetAddress.getByName("router.bittorrent.com");
int port = 6881;
DatagramPacket sendPacket = new DatagramPacket(sendBuffer, sendBuffer.length, addr, port);
client.send(sendPacket);
byte[] receiveBuf = new byte[512];
DatagramPacket receivePacket = new DatagramPacket(receiveBuf, receiveBuf.length);
client.receive(receivePacket);
System.out.println("Client Source Data:" + Arrays.toString(receivePacket.getData()));
String receiveData = new String(receivePacket.getData(), "UTF-8");
System.out.println("Client String Data:" + receiveData);
client.close();
return receivePacket.getData();
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
以UTF-8读取响应,但iso-8859-1也是乱码文本。
谁能帮助我,谢谢!
答案 0 :(得分:1)
服务器响应乱码文本
不,响应是bencoded并包含原始二进制数据 它不能被视为文本。
在BEP5中,要使示例中的原始二进制node_id可打印,
巧妙地选择它只包含字母数字字符
请参阅:
Bittorrent KRPC - Why are node ID's half the size of an info_hash and use every character a-z?
ip
密钥是在BEP42 - DHT Security extension
收到的回复完全有效。
TODO: Working Java code
答案 1 :(得分:0)
Map<String, Object> dict = bencode.decode(result, Type.DICTIONARY);
这为您提供了消息的解码根词典Map
。在其中你应该找到r
字典作为另一个地图,并在该地图中找到id
值。 id的类型取决于您正在使用的bedecoding库。
如果它是ByteBuffer
或byte[]
,那么你应该有20个字节可以进行hexencode(到40个字符),如果你需要它是人类可读的。 DHT协议处理原始哈希值,而不是十六进制值。
如果是String
,那么在对十六进制编码之前,您必须将字符串转换回byte[]
。只有当bdecoder使用 ISO 8859-1 进行解码时才有可能,因为该字符集是双向安全的,而utf-8不适用于任意字节序列。