Android UDP 返回随机字符

huangapple go评论76阅读模式
英文:

Android UDP returning random characters

问题

我试图制作一个通过UDP接收F1 2020遥测数据的应用程序。

不知何故,无论我如何尝试将接收到的字节转换为字符串,它都返回随机字符。

我确信这是一个新手的错误,但我只是找不到出错的地方。

这是接收并记录数据包的类:

class ClientListen implements Runnable {
    private Thread t;

    public void run() {
        boolean run = true;
        try {
            DatagramSocket udpSocket = new DatagramSocket(20777);
            byte[] buffer = new byte[2048];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            while (run) {
                udpSocket.receive(packet);
                String received = new String(packet.getData(), 0, packet.getLength());
                packet.setLength(buffer.length);
                Log.i("Received data", received);
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {}
            }
            udpSocket.close();

        } catch (IOException e) {
            Log.e("UDP client has IOException", "error: ", e);
            run = false;
        }
    }

    public void start() {
        if (t == null) {
            t = new Thread(this);
            t.start();
        }
    }
}

当我点击应用程序上的按钮时,线程开始运行,我可以看到它记录数据,并在游戏暂停时停止,这是应该的。

数据输出类似于I/Received data: �����R<�<li�)C�������GndBƨS@3Z��N��9��y�}�Ժ�~��g�r��~�ө;��J�qӼ��?�RQ=�k�<h���p�<@84�C�������...

我曾认为是因为我记录了字节而不是字符串,但我尝试了许多不同的方法来转换它为字符串,结果始终如此。

英文:

I was trying to make an app that receives telemetry of F1 2020 via UDP.

For some reason, no matter how I try to turn the received bytes into a string, it just returns random characters.

I'm sure its a noob mistake somewhere but I just can't figure out where

This is the class that receives and logs the packets:

class ClientListen implements Runnable {
    private Thread t;

    public void run() {
        boolean run = true;
        try {
            DatagramSocket udpSocket = new DatagramSocket(20777);
            byte[] buffer = new byte[2048];
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

            while (run) {
                udpSocket.receive(packet);
                String received = new String(packet.getData(), 0, packet.getLength());
                packet.setLength(buffer.length);
                Log.i("Received data", received);
                try{
                    Thread.sleep(50);

                } catch (InterruptedException e) {}
            }
            udpSocket.close();

        }catch (IOException e) {
            Log.e("UDP client has IOException", "error: ", e);
            run = false;
        }
    }

    public void start() {
        if (t == null) {
            t = new Thread();
            t.start();
        }
    }
}

The thread beggins when I click a button on the app, and I can see it logs data and stops when the game is paused, as it should

The data output is something like I/Received data: �����R<�<li�)C�������GndBƨS@3Z��N��9��y�}�Ժ�~��g�r��~�ө;��J�qӼ��?�RQ=�k�<h���p�<@84�C�������...

I thought it was beacause I was logging bytes and not a string, but I tried many different ways to turn in into a string and it always has this result.

答案1

得分: 0

由于某种原因,无论我如何尝试将接收到的字节转换为字符串,它都只返回随机字符。

当您执行 new String(bytes, ...) 时,您正在尝试将以字节编码的文本解码为 Unicode 字符... 使用您平台的默认字符集:

  • 如果字节表示的数据是以不同字符集编码的文本,则字符将无法正确解码。您需要使用正确的字符集。

  • 但如果字节中的数据是二进制而不是文本,无论您使用什么字符集,都不太可能将其转换为除了 "随机字符" 之外的任何东西,这是常见的数据包发送方式

UDP 数据包中发送的数据通常是二进制的。看起来这就是这种情况。


我确信这是一个初学者的错误,但我就是找不到错在哪里

我认为你的错误在于假设所有数据从根本上都是文本。

当存在关于UDP数据包格式的规范时,您应该首先阅读它。该格式将决定您应该如何解码数据包。

通过谷歌搜索,我找到了这个链接:

如果没有规范,您将不得不从示例数据包中进行逆向工程来确定格式。这是繁琐和耗时的工作,我们无法教您如何做。

英文:

> For some reason, no matter how I try to turn the received bytes into a string, it just returns random characters.

When you do new String(bytes, ...) you are attempting to decode text encoded as bytes into Unicode characters ... using your platform's default character set:

  • If the data represented by the bytes is text encoded in a different character set, the characters will not be decoded correctly. You need to use the correct character set.

  • But if the data in the bytes is binary rather than text, it is unlikely that you will be able to turn it into anything other than "random characters" by decoding this way, no matter what character set you are using.

If is common for data sent in UDP packets to be binary. It looks like that is the case here.


> I'm sure its a noob mistake somewhere but I just can't figure out where

I think that your mistake is assuming that all data is fundamentally text.

When there is a specification for the format of those UDP packets, you should start by reading it. The format will determine how you should decode the packets.

A Google search found this for me:

If there was no specification, you would have to reverse engineer the format from example packets. That is tedious and time-consuming, and it isn't something we can teach you to do.

huangapple
  • 本文由 发表于 2020年8月8日 05:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63309531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定