java发送UDP数据包问题

发布于 2024-12-03 15:38:03 字数 1752 浏览 0 评论 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

我的java程序:

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(27022);
            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("receive: " + new String(rec));

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

}

但是当我运行它时,我使用wireshark来捕获数据包,我得到这个:

在此处输入图像描述 前四个字节是0x3f而不是0xff,那么有什么问题呢?我在windows 7中文版上运行java 6。

guys! I am write a simple program to send a message to a Game server(Counter-Strike), that message is used to query server information, it has a fixed format:

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

My java program:

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(27022);
            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("receive: " + new String(rec));

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

}

but when I run it, I use wireshark to capture the packet, I got this :

enter image description here
the first four bytes are 0x3f not 0xff, so what's problem? I am running java 6 on windows 7 Chinese edition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素手挽清风 2024-12-10 15:38:03

通过 Stringchar[]byte[] 的转换不能保证无损,因为它涉及字符集转换。

尝试将 peer0_0[] 声明为 byte 数组并直接使用它。

The conversion from char[] to byte[] via String is not guaranteed to be lossless, since it involves charset conversions.

Try declaring peer0_0[] as a byte array and working with it directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文