Java 读取rcon命令响应

Java 读取rcon命令响应,java,Java,我想使用java向服务器发送rcon命令,为此我使用以下库 当我像这样运行命令时 Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes()); // Example: On a minecraft server this will list the connected players String result = rcon.command("list"); // Display the result in the co

我想使用java向服务器发送rcon命令,为此我使用以下库 当我像这样运行命令时

Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes());

// Example: On a minecraft server this will list the connected players
String result = rcon.command("list");

// Display the result in the console
System.out.println(result);
我的服务器在控制台中显示响应
Gc连接从…
建立,依此类推

但是在java应用程序中,我有一个空的结果,它不是空的,它只是空的

String result = rcon.command("list");
如何使用rcon协议从服务器获取响应?

尝试以下方法:

try {
    Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes());
    String result = rcon.command("list");
    System.out.println(result);
} catch (AuthenticationException e) {
    String result = "Authentication failed";
}
试试这个:

try {
    Rcon rcon = new Rcon("127.0.0.1", 27015, "mypassword".getBytes());
    String result = rcon.command("list");
    System.out.println(result);
} catch (AuthenticationException e) {
    String result = "Authentication failed";
}

最后,我编写了自己的实现:

public final class RconClient implements AutoCloseable {

    private static final int MAX_SIZE = 4096;

    private final Socket socket;

    private final RconData data;

    private static final Logger LOG = LoggerFactory.getLogger(RconClient.class);

    @SuppressWarnings("ConstructorShouldNotThrowExceptions")
    public RconClient(final String host,
                      final int port,
                      final byte[] password) throws IOException {

        this.socket = new Socket(host, port);

        final RconData requst = request(new RconData(RconData.AUTH, password));

        if (requst.getId() == -1) {
            LOG.error("Wrong password or ip to connect to rcon");
            throw new LoginException(host, port);
        }

        this.data = read();

    }



    public String command(String command) throws IOException {
        command = "get5_status";
        final RconData response = request(new RconData(command.getBytes()));
        return new String(response.getPayload(), Charset.forName("UTF-8"));
    }

    public RconData request(RconData packet) throws IOException {
        try {
            write(packet);
            return read();
        } catch (final SocketException exception) {
            socket.close();
            throw exception;
        }
    }

    private void write(RconData packet) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(packet.getPayload().length + 14);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        buffer.putInt(packet.getPayload().length + 10);

        buffer.putInt(packet.getId());
        buffer.putInt(packet.getType());
        buffer.put(packet.getPayload());

        buffer.put((byte)0);
        buffer.put((byte)0);

        socket.getOutputStream().write(buffer.array());
        socket.getOutputStream().flush();
    }

    private RconData read() throws IOException {
        byte[] packet = new byte[MAX_SIZE];
        int packetSize = this.socket.getInputStream().read(packet);

        ByteBuffer buffer = ByteBuffer.wrap(packet, 0, packetSize);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        if (buffer.remaining() < 4) {
            throw new WrongPacketException();
        }

        int size = buffer.getInt();

        if (buffer.remaining() < size) {
            throw new WrongPacketException();
        }

        int id = buffer.getInt();
        int type = buffer.getInt();

        byte[] payload = new byte[size - 10];
        buffer.get(payload);

        buffer.get(new byte[2]);

        return new RconData(id, type, payload);
    }

    @Override
    public void close() throws IOException {
        this.socket.close();
    }
}
public final类RconClient实现自动关闭{
私有静态最终整数最大值=4096;
专用终端插座;
私有最终RconData数据;
私有静态最终记录器LOG=LoggerFactory.getLogger(RconClient.class);
@抑制警告(“构造函数不应通过异常”)
公共RconClient(最终字符串主机,
最终国际端口,
最终字节[]密码)引发IOException{
this.socket=新套接字(主机、端口);
最终RconData请求=请求(新的RconData(RconData.AUTH,password));
if(requust.getId()=-1){
日志错误(“连接到rcon的密码或ip错误”);
抛出新的LoginException(主机、端口);
}
this.data=read();
}
公共字符串命令(字符串命令)引发IOException{
command=“get5_状态”;
最终RconData响应=请求(新的RconData(command.getBytes());
返回新字符串(response.getPayload(),Charset.forName(“UTF-8”);
}
公共RconData请求(RconData数据包)引发IOException{
试一试{
写入(数据包);
返回read();
}捕获(最终SocketException异常){
socket.close();
抛出异常;
}
}
私有无效写入(RconData数据包)引发IOException{
ByteBuffer buffer=ByteBuffer.allocate(packet.getPayload().length+14);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(packet.getPayload().length+10);
buffer.putInt(packet.getId());
buffer.putInt(packet.getType());
buffer.put(packet.getPayload());
buffer.put((字节)0);
buffer.put((字节)0);
socket.getOutputStream().write(buffer.array());
socket.getOutputStream().flush();
}
private RconData read()引发IOException{
字节[]数据包=新字节[最大大小];
int packetSize=this.socket.getInputStream().read(数据包);
ByteBuffer缓冲区=ByteBuffer.wrap(数据包,0,数据包大小);
buffer.order(ByteOrder.LITTLE_ENDIAN);
if(buffer.remaining()<4){
抛出新的错误包异常();
}
int size=buffer.getInt();
if(buffer.remaining()

其中RconData是带有
byte[]password
属性的简单POJO,

最后我编写了自己的实现:

public final class RconClient implements AutoCloseable {

    private static final int MAX_SIZE = 4096;

    private final Socket socket;

    private final RconData data;

    private static final Logger LOG = LoggerFactory.getLogger(RconClient.class);

    @SuppressWarnings("ConstructorShouldNotThrowExceptions")
    public RconClient(final String host,
                      final int port,
                      final byte[] password) throws IOException {

        this.socket = new Socket(host, port);

        final RconData requst = request(new RconData(RconData.AUTH, password));

        if (requst.getId() == -1) {
            LOG.error("Wrong password or ip to connect to rcon");
            throw new LoginException(host, port);
        }

        this.data = read();

    }



    public String command(String command) throws IOException {
        command = "get5_status";
        final RconData response = request(new RconData(command.getBytes()));
        return new String(response.getPayload(), Charset.forName("UTF-8"));
    }

    public RconData request(RconData packet) throws IOException {
        try {
            write(packet);
            return read();
        } catch (final SocketException exception) {
            socket.close();
            throw exception;
        }
    }

    private void write(RconData packet) throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(packet.getPayload().length + 14);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        buffer.putInt(packet.getPayload().length + 10);

        buffer.putInt(packet.getId());
        buffer.putInt(packet.getType());
        buffer.put(packet.getPayload());

        buffer.put((byte)0);
        buffer.put((byte)0);

        socket.getOutputStream().write(buffer.array());
        socket.getOutputStream().flush();
    }

    private RconData read() throws IOException {
        byte[] packet = new byte[MAX_SIZE];
        int packetSize = this.socket.getInputStream().read(packet);

        ByteBuffer buffer = ByteBuffer.wrap(packet, 0, packetSize);
        buffer.order(ByteOrder.LITTLE_ENDIAN);

        if (buffer.remaining() < 4) {
            throw new WrongPacketException();
        }

        int size = buffer.getInt();

        if (buffer.remaining() < size) {
            throw new WrongPacketException();
        }

        int id = buffer.getInt();
        int type = buffer.getInt();

        byte[] payload = new byte[size - 10];
        buffer.get(payload);

        buffer.get(new byte[2]);

        return new RconData(id, type, payload);
    }

    @Override
    public void close() throws IOException {
        this.socket.close();
    }
}
public final类RconClient实现自动关闭{
私有静态最终整数最大值=4096;
专用终端插座;
私有最终RconData数据;
私有静态最终记录器LOG=LoggerFactory.getLogger(RconClient.class);
@抑制警告(“构造函数不应通过异常”)
公共RconClient(最终字符串主机,
最终国际端口,
最终字节[]密码)引发IOException{
this.socket=新套接字(主机、端口);
最终RconData请求=请求(新的RconData(RconData.AUTH,password));
if(requust.getId()=-1){
日志错误(“连接到rcon的密码或ip错误”);
抛出新的LoginException(主机、端口);
}
this.data=read();
}
公共字符串命令(字符串命令)引发IOException{
command=“get5_状态”;
最终RconData响应=请求(新的RconData(command.getBytes());
返回新字符串(response.getPayload(),Charset.forName(“UTF-8”);
}
公共RconData请求(RconData数据包)引发IOException{
试一试{
写入(数据包);
返回read();
}捕获(最终SocketException异常){
socket.close();
抛出异常;
}
}
私有无效写入(RconData数据包)引发IOException{
ByteBuffer buffer=ByteBuffer.allocate(packet.getPayload().length+14);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(packet.getPayload().length+10);
buffer.putInt(packet.getId());
buffer.putInt(packet.getType());
buffer.put(packet.getPayload());
buffer.put((字节)0);
buffer.put((字节)0);
socket.getOutputStream().write(buffer.array());
socket.getOutputStream().flush();
}
private RconData read()引发IOException{
字节[]数据包=新字节[最大大小];
int packetSize=this.socket.getInputStream().read(数据包);
ByteBuffer缓冲区=ByteBuffer.wrap(数据包,0,数据包大小);
buffer.order(ByteOrder.LITTLE_ENDIAN);
if(buffer.remaining()<4){
抛出新的错误包异常();
}
int size=buffer.getInt();
if(buffer.remaining()
其中RconData是带有
byte[]passwo的简单POJO