-2

I have a java program written that simply receives data sent through a UDP socket and prints it to the screen. The data that is received is sent from another computer that uses matlab to send the data. I am pretty sure that the matlab simulation is fine because all it does is take a constant value of 2 with a double data type, uses a byte packer to pack the data and sends it through a UDP block to the specified IP Address and port. I concurrently run the java code on another pc which receives the data packet as a byte array of length 1024.

The data is received just fine, however the value that is constantly printed is

4.7783097267364807 E -299.

I am not sure if my code is grabbing the desired data from the packet correctly or if the ByteBuffer.wrap(.....).getDouble() function is used correctly. Any help would be greatly appreciated

 import java.io.*;
 import java.net.*;
 import java.nio.ByteBuffer;

class receiver
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(1024);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            System.out.println("Listening...");

            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  double data = ByteBuffer.wrap(receiveData).getDouble();
                  System.out.println(data);
               }
      }
}
Flower
  • 249
  • 1
  • 6
  • 14
  • 1
    Define 'uses a byte packer to pack the data', and provide a hex dump of the first 8 or 16 bytes of the received datagram. – user207421 Jul 18 '17 at 23:28

1 Answers1

1

Your code is correct given the specification, but Matlab is sending you 0x0200000000000000 (or something shorter: see below). The big-endian representation of 2.0 as a double is 0x4000000000000000, so they are certainly not sending you a double.

To get 2 from what they're actually sending you, do this:

ByteBuffer  bb = ByteBuffer.wrap(receivePacket.getData(), 0, receivePacket.getLength());
bb.order(ByteOrder.LITTLE_ENDIAN);

Then try the following, separately, in this order, until one works:

long l = bb.getLong();
int i = bb.getInt();
short s = bb.getShort();
byte b = bb.getByte();

without a buffer underflow. The way you're wrapping the buffer you are ignoring the datagram length, so it is impossible to tell whether they are sending one, two, four, or eight bytes.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • Thanks alot. It turns out byte b = bb.getByte(); is what works. The other statements cause an underflow exception. So, what do the wrap function and byteorder functions do exactly? – Flower Jul 19 '17 at 19:15
  • Could you please tell me how I would go about doing this if I want to send data from my pc to the other pc running matlab in this way? – Flower Jul 20 '17 at 00:00
  • 1
    So they sent a byte, not a double. The wrap method I used wrapped the actual datagram up to its actual length, as obtained from the datagram. The little-endian method is documented, but you may not need it, depending on what format Matlab is expecting, which I cannot advise you on: you will have to discover what Matlab is really expecting to receive for yourself. – user207421 Jul 20 '17 at 00:54