将DHT查询发送到"router.bittorrent.com",响应乱码 [英] Send DHT queries to "router.bittorrent.com" response garbled text

查看:207
本文介绍了将DHT查询发送到"router.bittorrent.com",响应乱码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 bep_0005 页中阅读了DHT协议.

I read the DHT Protocol in bep_0005 page.

但是,当我发送ping查询或find_node查询时,服务器会显示乱码(都是 router.bittorrent.com:6881 dht.transmissionbt.com:6881 )

But when I send a ping query or a find_node query, the server response a garbled text (both of router.bittorrent.com:6881 or dht.transmissionbt.com:6881)

这是下面的Java源代码

Here is the Java source code bellow

    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"}}

ping Query = {"t":"aa", "y":"q", "q":"ping", "a":{"id":"abcdefghij0123456789"}}

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

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

向bep_0005协议添加响应,如下所示:

响应= {"t":"aa","y":"r","r":{"id":"mnopqrstuvwxyz123456"}}

Response = {"t":"aa", "y":"r", "r": {"id":"mnopqrstuvwxyz123456"}}

bencoded = d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re

bencoded = d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re

但我的答复是:

响应= {ip = P ,r = {id =2.NisQ J )ͺ F | g},t = tr,y = r}

Response = {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

bencoded = d2:ip6:��P���1:rd2:id20:2�NisQ�J�)ͺ����F|�ge1:t2:tr1:y1:re

发送udp部分的Java代码是:

Send udp part Java code is:

    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也是乱码.

read the response in UTF-8 , but the iso-8859-1 is also a garbled text.

谁能帮助我,谢谢!

推荐答案

服务器回复乱码

the server response a garbled text

否,响应将被编码并包含原始二进制数据.
不能将其视为文本.

No, the response is bencoded and contains raw binary data.
It CAN NOT be treated as text.

在BEP5中,要使示例中的原始二进制node_id可打印, 巧妙地选择了它仅包含字母数字字符.
参见:

In BEP5, to make the raw binary node_id in the examples printable, it has cleverly been chosen to consist of only alphanumeric characters.
See:
Bittorrent KRPC - Why are node ID's half the size of an info_hash and use every character a-z?

ip键是在以下内容中说明的扩展: BEP42-DHT安全扩展

The ip key is a extension explained in: BEP42 - DHT Security extension

收到的响应是完全有效的.

The received response is fully valid.

TODO: Working Java code

这篇关于将DHT查询发送到"router.bittorrent.com",响应乱码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆