gpt4 book ai didi

java - 发送DHT查询到 "router.bittorrent.com"响应乱码

转载 作者:行者123 更新时间:2023-12-01 19:06:36 26 4
gpt4 key购买 nike

我在 bep_0005 页面阅读了 DHT 协议(protocol)。

但是当我发送 ping 查询或 find_node 查询时,服务器响应乱码文本(router.bittorrent.com:6881dht.transmissionbt.com:6881)

下面是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 查询 = {"t":"aa", "y":"q", "q":"ping", "a":{"id":"abcdefghij0123456789"}}

bencoded = d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe

根据 bep_0005 协议(protocol),响应如下:

响应 = {"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也是乱码。

谁能帮帮我,谢谢!

最佳答案

the server response a garbled text

不,响应经过编码并包含原始二进制数据。
它不能被视为文本。

在 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

关于java - 发送DHT查询到 "router.bittorrent.com"响应乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59542765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com