伙计们,我正在尝试编写一个简单的 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/