0

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);
    }
marina
  • 1
  • 2

3 Answers3

1

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.

hcarrasko
  • 2,320
  • 6
  • 33
  • 45
  • Thanks Hector, I got hexa strings, but still no luck with this, this data just doesn't make any sense. In the documentation of the server sending the data it is stated: "All fields are float datatype (in C#)". I'm trying to convert stuff I get in anything that comes to mind, but I'm still stuck – marina Mar 30 '15 at 21:10
0

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...

Community
  • 1
  • 1
bdulac
  • 1,686
  • 17
  • 26
  • Thanks, I've encoutered the link before, but it didn't help, so I wrote the question. Code is available now... – marina Mar 31 '15 at 20:30
0

You need:

  1. A specification of the packet format you are receiving.
  2. A DataInputStreamwrapped 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 (i.e 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks, I've tried with a lot of ways to solve this, but the thing is, I don't have the source of transmitter, all I know that there is a sequence of numbers (floating point) I need to read, on certain port on my localhost. I've updated the post with code... – marina Mar 31 '15 at 20:32
  • You don't state what the problem is with your edited code, but your new code reads one float per datagram. Your description reads like there are multiple floats per datagram, anyway it can't hurt to loop reading them all as I described (slightly improved by an edit). – user207421 Mar 31 '15 at 23:55