Java向我的世界服务器发送握手包

标签 java network-programming minecraft packets

我一直在开发一个 java 程序,它基本上像 Minechat(基于文本的应用程序,只查看聊天。)我从来没有真正接触过太多网络,所以问题是弄清楚如何正确发送数据包。我目前正在与服务器创建握手。经过几个小时的研究,我想出了以下代码,但它总是遇到“失败!(异常)”消息。对我来说,一切看起来都是正确的,但就我所知,它可能是 100% 错误的。如果有人能指出我在这里做错了什么,我将不胜感激。

供引用,随意使用thisthis .

public static void main(String[] args) throws IOException {
    host = new InetSocketAddress("162.244.165.111", 48040);
    socket = new Socket();
    System.out.println("Connecting...");
    socket.connect(host, 3000);
    System.out.println("Done!");
    System.out.println("Making streams...");
    output = new DataOutputStream(socket.getOutputStream());
    input = new DataInputStream(socket.getInputStream());
    System.out.println("Done!");
    System.out.println("Attempting handshake... "+host.getAddress().toString().substring(1));
    byte[] msg = ("47;"+host.getAddress().toString().substring(1)+";"+host.getPort()+";2;").getBytes(Charset.forName("UTF-16"));
    output.writeInt(msg.length+Integer.valueOf(0x00));
    output.writeByte(0x00);
    output.write(msg);
    output.flush();
    try {
        if (input.readByte() != 0x02)
            System.out.println("Failed!");
        else
            System.out.println("Done!");
    } catch (EOFException e) {
        System.out.println("Failed! (Exception)");
    }
}

编辑: 更多研究建议我使用字节数组,但这让我对如何表示字符串和使用字符串感到困惑?

最佳答案

查看此页http://wiki.vg/Protocol看起来您没有写入足够的数据,也没有以正确的顺序写入。您还需要使用 varint这是整数的一种特殊类型的数据表示。

本期相关链接:


状态 ping 的工作方式如下:

C->S : Handshake State=1
C->S : Request
S->C : Response
C->S : Ping
S->C : Pong

C 是客户端,S 是服务器

使用 wiki 和提供的代码示例,我修改了您的代码以遵循整个状态请求。

public static void main(String [] args) throws IOException {
    String address = "162.244.165.111";
    int port = 48040;

    InetSocketAddress host = new InetSocketAddress(address, port);
    Socket socket = new Socket();
    System.out.println("Connecting...");
    socket.connect(host, 3000);
    System.out.println("Done!");
    System.out.println("Making streams...");
    DataOutputStream output = new DataOutputStream(socket.getOutputStream());
    DataInputStream input = new DataInputStream(socket.getInputStream());

    System.out.println("Done!");
    System.out.println("Attempting handshake... "+host.getAddress().toString());


    byte [] handshakeMessage = createHandshakeMessage(address, port);

    // C->S : Handshake State=1
    // send packet length and packet
    writeVarInt(output, handshakeMessage.length);
    output.write(handshakeMessage);

    // C->S : Request
    output.writeByte(0x01); //size is only 1
    output.writeByte(0x00); //packet id for ping


    // S->C : Response
    int size = readVarInt(input);
    int packetId = readVarInt(input);

    if (packetId == -1) {
        throw new IOException("Premature end of stream.");
    }

    if (packetId != 0x00) { //we want a status response
        throw new IOException("Invalid packetID");
    }
    int length = readVarInt(input); //length of json string

    if (length == -1) {
        throw new IOException("Premature end of stream.");
    }

    if (length == 0) {
        throw new IOException("Invalid string length.");
    }

    byte[] in = new byte[length];
    input.readFully(in);  //read json string
    String json = new String(in);


    // C->S : Ping
    long now = System.currentTimeMillis();
    output.writeByte(0x09); //size of packet
    output.writeByte(0x01); //0x01 for ping
    output.writeLong(now); //time!?

    // S->C : Pong
    readVarInt(input);
    packetId = readVarInt(input);
    if (packetId == -1) {
        throw new IOException("Premature end of stream.");
    }

    if (packetId != 0x01) {
        throw new IOException("Invalid packetID");
    }
    long pingtime = input.readLong(); //read response


    // print out server info
    System.out.println(json);

    System.out.println("Done!");
}

public static byte [] createHandshakeMessage(String host, int port) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    DataOutputStream handshake = new DataOutputStream(buffer);
    handshake.writeByte(0x00); //packet id for handshake
    writeVarInt(handshake, 4); //protocol version
    writeString(handshake, host, StandardCharsets.UTF_8);
    handshake.writeShort(port); //port
    writeVarInt(handshake, 1); //state (1 for handshake)

    return buffer.toByteArray();
}

public static void writeString(DataOutputStream out, String string, Charset charset) throws IOException {
    byte [] bytes = string.getBytes(charset);
    writeVarInt(out, bytes.length);
    out.write(bytes);
}

public static void writeVarInt(DataOutputStream out, int paramInt) throws IOException {
    while (true) {
        if ((paramInt & 0xFFFFFF80) == 0) {
          out.writeByte(paramInt);
          return;
        }

        out.writeByte(paramInt & 0x7F | 0x80);
        paramInt >>>= 7;
    }
}

public static int readVarInt(DataInputStream in) throws IOException {
    int i = 0;
    int j = 0;
    while (true) {
        int k = in.readByte();
        i |= (k & 0x7F) << j++ * 7;
        if (j > 5) throw new RuntimeException("VarInt too big");
        if ((k & 0x80) != 128) break;
    }
    return i;
}

关于Java向我的世界服务器发送握手包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30768091/

相关文章:

java - 将我的依赖项放入我的 JAR 中

java - 测试序列化编码

c# - 如何从服务器优雅地关闭套接字

java - 解密我的世界 .dat 文件

java - 在此实例中正确使用 "for"

java - 服务器模拟器的设计模式

java - Dijkstra 算法 : How do you choose the edge source and destination

java - 通过 LAN 访问共享资源

python - 通过 Mininet 网络发送 "random"流量

java - 导入类 Java 问题 Minecraft 插件