1

我将来自 router.bittorrent.com 的 find_node 响应的节点解码为字符串,并向解码的“节点”发送了一个 find_node 请求,但我从未修改过来自该“节点”的 find_node 响应,我怀疑解码“节点”的方式是错误的,这是代码:

        byte[] nodesBytes = ((String)nodes).getBytes();
        ByteBuffer buffer = ByteBuffer.wrap(nodesBytes);
        int size = nodesBytes.length / 26;
        for (int i = 0; i < size; i++) {

            byte[] bytes = new byte[26];
            byte[] nodeIdBytes = Arrays.copyOfRange(bytes, 0, 20);
            byte[] ipBytes = Arrays.copyOfRange(bytes, 20, 24);
            byte[] portBytes = Arrays.copyOfRange(bytes, 24, 26);
            RoutingTable.RoutingNode routingNode = new RoutingTable.RoutingNode();
            try {
                routingNode.nodeId = nodeIdBytes;
                routingNode.ip = InetAddress.getByAddress(ipBytes);
                routingNode.port = (((((short)portBytes[1]) << 8) & 0xFF00) + (((short)portBytes[0]) & 0x00FF));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            send(routingNode);
        }

解码字符串代码是

  private static String decodeString(ByteBuffer byteBuffer) {
    try {
        StringBuilder buffer = new StringBuilder();
        int type = byteBuffer.get();
        buffer.append((char) type);
        do {
            byte a = byteBuffer.get();
            if (a == SEPARATOR) {
                break;
            } else {
                buffer.append((char) a);
            }
        } while (true);

        int length = Integer.parseInt(buffer.toString());
        byte[] bytes = new byte[length];
        byteBuffer.get(bytes);
        String value = new String(bytes, "UTF-8");
        logger.debug(value);
        return value;
    } catch (Exception e) {
        logger.error("", e);
    }
    return "";
}

有什么问题吗?

PS:send() 函数运行良好。

4

1 回答 1

0

((String)nodes).getBytes();

这假设了一个特定的编码,这可能不适合这种情况。这取决于您使用的 bdecoder 实现是做什么的。理想情况下,您应该使用一种直接从编码数据返回 abyte[]或的方法ByteBuffer,而无需经过String

routingNode.port = (((((short)portBytes[1]) << 8) & 0xFF00) + (((short)portBytes[0]) & 0x00FF));

您应该使用|而不是+. Additionalyshort是 java 中的有符号类型,但端口在 0-65535 的无符号范围内,因此您应该扩展为int。并且网络格式是 bigendian,所以端口的最高有效位在第 0 个字节,下半部分在第一个字节,所以你也把它倒过来了。

使用ByteBuffer而不是byte[]像我在自己的实现中所做的那样可以大大减少出错的可能性,因为它允许您直接获得一个short,然后将其转换为一个无符号整数。

于 2017-08-15T15:57:50.173 回答