首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java将握手数据包发送到minecraft服务器

Java将握手数据包发送到minecraft服务器
EN

Stack Overflow用户
提问于 2015-06-11 06:03:16
回答 1查看 10K关注 0票数 6

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

如需参考,请随意使用thisthis

代码语言:javascript
复制
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)");
    }
}

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-11 22:58:39

看一下这个页面,http://wiki.vg/Protocol,看起来你写的数据不够多,顺序也不正确。您还需要使用varint,它是整数的一种特殊数据表示类型。

此问题的相关链接:

  • Handshake Protocol
  • Packet format
  • Server Ping Explanation and Example (包括握手)

状态ping的工作原理如下:

代码语言:javascript
复制
C->S : Handshake State=1
C->S : Request
S->C : Response
C->S : Ping
S->C : Pong

C是客户端,S是服务器

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

代码语言:javascript
复制
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;
}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30768091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档