使用java查询反恐精英1.6服务器信息
我正在尝试编写一个简单的java程序来查询Counter-Strike 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();
}
}
}
发送查询消息后,它没有收到任何东西,并且程序不会退出。 我确信 219.133.59.20:27021 上有一个服务器 我的代码有什么问题吗?
guys I am trying to write a simple java program to query counter-strike 1.6 server:
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();
}
}
}
after sending query msg, it did not receive any thing, and the program will not exit.
I am sure there is a server on 219.133.59.20:27021
what's wrong with my code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我运行你的代码没有任何问题。输出如下:
我怀疑您的防火墙/路由器有问题。来自 http://download.oracle.com/javase/tutorial/networking/ datagrams/index.html:
I ran your code with no issues. The output is as follows:
I suspect you're having issues with your firewall/router. From http://download.oracle.com/javase/tutorial/networking/datagrams/index.html: