简体   繁体   中英

processing bytes received from UDP port in java

I am trying to read data from UDP port on localhost using Java. I'm pretty good with Java, but I can't solve this for quite a while now...

The thing is, after I connect using DatagramSocket and receive a packet with DatagramPacket , I get some bytes that have no sence, I can't see connection with the data I expect. Printout looks like this:

$őZAŇ"¤E€^ĽxΕ’M@ŢúCîS5;Ń8†8Ŕ$5»ôxŕ¸Ţf+?’Ť;Ů%>ż?>żA€ĹĽ‘_

so, I'm obviously handlig something in the wrong way. I've also read some signed/unsigned data problems with Java.

About a year ago I've created a similar app using C#, everything went pretty smooth.

Really hope someone can help.

Here is the code (one of the versions, I've tried a lot of different solutions)

 DatagramSocket mySocket = new DatagramSocket(null);
    InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 20777);
    mySocket.bind(addr);
    byte[] receiveData = new byte[152];
    while(true)
    {
        DatagramPacket receivePacket = new DatagramPacket(receiveData, 0, receiveData.length);
        mySocket.receive(receivePacket);
        byte[] barray = receivePacket.getData();

        ByteArrayInputStream inputStream = new ByteArrayInputStream(barray);
        DataInputStream dInputStream = new DataInputStream(inputStream);
        float a = dInputStream.readFloat();
        System.out.println(a);
    }

Using this method you can convert a byte array to hexadecimal string representation.

private String bytesToHex(byte[] bytes) {
    char[] hexArray = "0123456789ABCDEF".toCharArray();
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

Hope it helps.

I won't flag your question as a duplicate because it is your first one, but I think you should refer to this other exchange . A very elegant and clear solution to your problem is available.

By the way, a citation of the code reading the section you printed would have been welcome. Good luck...

You need:

  1. A specification of the packet format you are receiving.
  2. A DataInputStream wrapped around a ByteArrayInputStream wrapped around the byte array you used to build the DatagramPacket, not forgetting to use the constructor that takes an offset and length, which you get from the DatagramPacket.
  3. Code that calls the appropriate DataInputStream methods corresponding to (1).

At the moment you don't even appear to have (1). Without that, you haven't got a hope. Just trying to 'make sense' of binary data, especially by just printing it, is a complete waste of your time.

EDIT If, as per your comment, all the fields are floats , just loop over the datagram calling DataInputStream.readFloat() until it throws EOFException :

try
{
    while (true)
    {
        float f = dataInputStream.readFloat();
        System.out.println(f);
    }
}
catch (EOFException exc)
{
    // expected
}

If that doesn't work (ie produce recognizable value), you will have to switch to DatagramSocketChannel and ByteBuffer and experiment with the different byte-order possibilites.

Why you were trying to print floating-point data as though it was text remains a mystery.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM