如何正确查询服务器信息?

时间:2018-04-25 09:57:44

标签: java string byte

我想查询游戏服务器的播放器数量。此图显示了您可以从服务器获取的信息类型。 types

到目前为止,我正在使用此代码:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;

public static void main(String[] args) { 
    String hostname = "85.190.155.70";
    int port = 27017;      
    try {
        InetAddress address = InetAddress.getByName(hostname);
        DatagramSocket socket = new DatagramSocket();     
            byte[] readonly = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x54, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6E, 0x67, 0x69, 0x6E, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x00 };
            DatagramPacket request = new DatagramPacket(readonly, readonly.length, address, port);
            socket.send(request);
            byte[] receiveData = new byte[900];
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            socket.receive(receivePacket);
            String sentence = new String( receivePacket.getData(), 0, receivePacket.getLength(), "utf-8");
            System.out.println("RECEIVED: " + sentence);
            socket.close();        
    } catch (SocketTimeoutException ex) {
        System.out.println("Timeout error: " + ex.getMessage());
        ex.printStackTrace();
    } catch (IOException ex) {
        System.out.println("Client error: " + ex.getMessage());
        ex.printStackTrace();
    } 
}

我的问题是,我显然只是获取字符串类型(图像中的那些;服务器名称,地图,文件夹,游戏,版本)作为响应。以下是我得到的内容(无法复制粘贴):response

我需要更改什么才能将玩家数量作为回应?

其他信息: 服务器正在使用Valve's Server Query Protocol

1 个答案:

答案 0 :(得分:0)

我解决并分享 我的代码不好

public static void main(String[] args) { 
    ArrayList<String> list = new ArrayList<String>();
    DatagramSocket socket = null;
    try {

        String ip = null;
        if (Server[3].equals("1")) {
            ip = GetIP(Server[2]);
        } else {
            ip = Server[2];
        }
        int port = Integer.parseInt(Server[4]);
        InetAddress address = InetAddress.getByName(ip);
        byte[] readonly = { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x54, (byte) 0x53,
                (byte) 0x6F, (byte) 0x75, (byte) 0x72, (byte) 0x63, (byte) 0x65, (byte) 0x20, (byte) 0x45,
                (byte) 0x6E, (byte) 0x67, (byte) 0x69, (byte) 0x6E, (byte) 0x65, (byte) 0x20, (byte) 0x51,
                (byte) 0x75, (byte) 0x65, (byte) 0x72, (byte) 0x79, (byte) 0x00 };
        DatagramPacket request = new DatagramPacket(readonly, readonly.length, address, port);
        socket = new DatagramSocket();
        socket.send(request);
        byte[] receiveData = new byte[1400];
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
        socket.setSoTimeout(2000);
        socket.receive(receivePacket);

        int headlength = 4;
        int protocollength = 2;

        int i;
        int namelength = 0;
        for (i = 6; i < 1400; i++) {
            if (receiveData[i] == 0) {
                namelength = i - 6;
                break;
            }
        }
        byte[] name = new byte[namelength];
        for (i = 6; i < namelength + 6; i++) {
            name[i - 6] = receiveData[i];
        }
        String nameString = new String(name, StandardCharsets.UTF_8);

        int maplength = 0;
        for (i = 6 + namelength + 1; i < 1400; i++) {
            if (receiveData[i] == 0) {
                maplength = i - 6 - 1 - namelength;
                break;
            }
        }
        byte[] map = new byte[maplength];
        for (i = 6 + 1 + namelength; i < namelength + maplength + 6 + 1; i++) {
            map[i - 6 - 1 - namelength] = receiveData[i];
        }
        String mapString = new String(map, StandardCharsets.UTF_8);

        int folderlength = 0;
        for (i = 6 + 1 + 1 + namelength + maplength; i < 1400; i++) {
            if (receiveData[i] == 0) {
                folderlength = i - 6 - 1 - 1 - namelength - maplength;
                break;
            }
        }

        int gamelength = 0;
        for (i = 6 + 1 + 1 + 1 + namelength + maplength + folderlength; i < 1400; i++) {
            if (receiveData[i] == 0) {
                gamelength = i - 6 - 1 - 1 - 1 - namelength - maplength - folderlength;
                break;
            }
        }

        int IDlength = 1;

        byte players = receiveData[6 + namelength + maplength + folderlength + gamelength + 4 + 1 + 1];

        byte playersMax = receiveData[1 + 6 + namelength + maplength + folderlength + gamelength + 4 + 1 + 1];

        list.add(nameString);
        list.add(mapString);
        list.add(String.valueOf(players));
        list.add(String.valueOf(playersMax));
        list.add("1");
        return list;
    }
}