I am creating a class object to byte array see here, and passing this array to DatagramPacket to send at destination by means of UDP communication. At the destination side there is C application running and I am getting wired character. I damn sure this related to byte order. Because I am able to convert struct of C into class while receiving packet, but not able to do vice versa. Following is the sudo – code:
// recieving part
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet)
UdpPacket udp = new UdpPacket(buffer);
class UdpPacket implements Serializable
{
int a;
byte []message = new byte[10];
public UdpPacket(byte[] data)
{
ByteBuffer bb = ByteBuffer.wrap(data);
bb.order(ByteOrder.nativeOrder());
a = bb.getInt();
bb.get(message);
}
}
// sending ..
Converting UdpPacket object to byte array as per above mentioned link, but at the destination there is always junk values
DatagramSocket clientSocket = new DatagramSocket();
byte [] respoBytes = ObjectSerializer.serializeObject(udpPacket);
ByteBuffer bb = ByteBuffer.wrap(respoBytes);
byte []test = new byte[1116];
bb.order(ByteOrder.nativeOrder()); // tried all the orders here
bb.get(test);
DatagramPacket sendPacket = new DatagramPacket(test,test.length,client, Integer.parseInt(TxtFdPort.getText().toString()));
clientSocket.send(sendPacket)
;
Print out the sequence of bytes before sending, and after receiving. This will tell you if they are identical. Then inspect the bytes corresponding to e.g. an
int, and you will immediately see if they are in the correct order or not.It’s hard to be more specific, since you’re not showing the sending code.