java - 使用java查询反恐精英1.6服务器信息

标签 java udp

伙计们,我正在尝试编写一个简单的 java 程序来查询反恐精英 1.6 服务器:

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

public class Test {

    private static DatagramSocket ds;

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            ds = new DatagramSocket(27024);
            byte[] data;
            // TSource Engine Query
            char peer0_0[] = { 
                0xff, 0xff, 0xff, 0xff, 
                0x54, 0x53, 0x6f, 0x75,
                0x72, 0x63, 0x65, 0x20, 
                0x45, 0x6e, 0x67, 0x69, 
                0x6e, 0x65, 0x20, 0x51, 
                0x75, 0x65, 0x72, 0x79, 0x00 
            };
            data = new String(peer0_0).getBytes();

            System.out.println("send:" + new String(data));

            DatagramPacket dp = new DatagramPacket(data, 0, data.length, InetAddress.getByName("219.133.59.20"), 27021);
            ds.send(dp);
            byte[] rec = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(rec, 1024);
            ds.receive(dp2);

            System.out.println("revice:" + new String(rec));

            ds.close();
        } catch (IOException e) {
            e.printStackTrace();
            if(ds != null) ds.close();
        }
    }

}

发送query msg后,没有收到任何东西,程序不会退出。 我确定 219.133.59.20:27021 上有服务器 我的代码有什么问题?

最佳答案

我运行您的代码没有任何问题。输出结果如下:

send:ÿÿÿÿTSource Engine Query
revice:ÿÿÿÿm127.0.0.1:27021CoVerT战队深圳6å?·ç–¯å?‹è®°æœ?务器ã€?KP】

我怀疑您的防火墙/路由器有问题。来自 http://download.oracle.com/javase/tutorial/networking/datagrams/index.html :

Note: Many firewalls and routers are configured not to allow UDP packets. If you have trouble connecting to a service outside your firewall, or if clients have trouble connecting to your service, ask your system administrator if UDP is permitted.

关于java - 使用java查询反恐精英1.6服务器信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342685/

相关文章:

java - Java中如何命名具有相似语义的类?

java - 是否可以在 Android 应用程序上使用 MARY TTS 引擎?有没有人尝试过这个?从哪里开始比较好?

java - Spring @Secured 和 @PreAuthorize 在普通(非 Web)应用程序中没有效果

java - getter 的良好命名约定

python - 从 udp 接收符号,为什么?

java - 当您的应用程序通过通知打开时,是否可以从打开的位置返回到上一个应用程序?安卓

Android 在发送 UDP 数据包时抛出异常

安卓模拟器udp广播

java - 如何用 Java 编写自己的桌面共享应用程序?

php - PHP-获取数据包长度