无法将minecraft登录数据包发送到服务器? [英] Cant send minecraft login packet to server?

查看:161
本文介绍了无法将minecraft登录数据包发送到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Minecraft是一款也可以在多人服务器上玩的游戏.每个服务器都有自己的IP,每个服务器的端口为"25565".在Generell中,对于此问题,您应该熟悉Minecaft协议( https://wiki.vg/Protocol#Login_Start ).即使没有,我也创建了一个超链接,您可以在其中查找.我的目标是创建一个Minecraft聊天机器人,甚至无需打开Minecraftlauncher即可加入任何服务器.我知道,已经有很多这样的命令了,但是我想创建一些客户端应该发送的新命令.

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"作为错误消息,将用户名发送到服务器.

In generell, there are two big steps of the joining process when you join a Minecraft server. First, you need a connection sending a handshake (state 1) and "ping-pong". After this, you are connected to the server. This first step works very well, so I think I needn´t to show you. But the second step is the authentification of every client. Herefor I send a handshake (state 2) and then there comes my problem : I always get "java.io.EOFException" as a error message on my second step, sending my username to the Server.

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);
}

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

So, as you can see quite a complicated thing I guess. If somebody could answer me, why I´m getting this error message and how to fix it i would be very very happy! Thank you

我找到了一个帖子,它对我的​​联系很有帮助! Java将握手数据包发送到Minecraft服务器

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

推荐答案

当您的数据包包含整个字符串"ExamplePlayer"时,您正在编写一个数据包长度前缀1.

You're writing a packet length prefix of 1, when your packet contains the entire string "ExamplePlayer".

相反,应像在 createHandshakeMessageLogin 函数中那样构造数据包,然后在发送该缓冲区的内容之前,将长度作为varint发送.

Instead, construct the packet like is done in the createHandshakeMessageLogin function, then send the length as a varint before sending the content of that buffer.

这篇关于无法将minecraft登录数据包发送到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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