无法将minecraft登录数据包发送到服务器?

Minecraft is a game which also can be played on multiplayer servers. Each server has its own IP and the port is for every server "25565". In generell, for this problem you should be familiar with Minecaft protocol(https://wiki.vg/Protocol#Login_Start). Even if not, I created a hyperlink where you can look for this. My goal is to create a Minecraft Chatbot, without even open Minecraftlauncher to join any server. I know, there are already a lot of these existing, but I want to create some new commands which the client should send.

在Generell中,加入Minecraft服务器时,加入过程有两个重要步骤。首先,您需要一个发送握手(状态1)和“乒乓”的连接。之后,您已连接到服务器。第一步效果很好,所以我不需要告诉你。但是第二步是对每个客户端进行身份验证。为此,我发送了一个握手信号(状态2),然后出现了我的问题:在第二步中,我总是收到“ java.io.EOFException”作为错误消息,将用户名发送到服务器。

try {
private String host = "hypixel.net";
socket.connect(host, 25565); //I created already a Socket called "socket"
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
DataInputStream input = new DataInputStream(socket.getInputStream()); //socket is for creating streams
byte[] handShakeMessage = createHandshakeMessageLogin(host, 25565);
writeVarInt(output, handShakeMessage.length);
output.write(handShakeMessage);
System.out.println("Send handShakeMessage!");

output.writeByte(0x01); //hopefully the right packet size
output.writeByte(0x00); //packetID
output.writeUTF("ExamplePlayer"); //sending username 
}

    public 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 byte [] createHandshakeMessageLogin(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, 2); //state (2 for login)

    return buffer.toByteArray();
}

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

因此,正如您所看到的,我猜很复杂。如果有人可以回答我,为什么我收到此错误消息以及如何解决它,我将非常高兴!谢谢

EDIT: I found a post, which helped me a lot with the connection! Java sending handshake packets to minecraft server

评论