Solution 1 :
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.
Problem :
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ƨ[email protected]��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.
Comments
Comment posted by forums.codemasters.com/topic/…
Did you really expect to get palintext data over udp? Maybe this can help you:
Comment posted by Don Foumare
Is this about the game from codemasters?
Comment posted by user207421
The sleep is pointless. Remove it.
Comment posted by IVSOP
I’m new to programming and I was sure I was just being dumb somewhere. I know it looks like a stupid question now that I know what I did wrong, so thanks a lot for the help.