Strange problem with DatagramSocket and DatagramPacket.
Hi!
I got a strange problem with UDP sockets:
Client (hardware device) send binary data messages, consisting of bytes and shorts fields (8 and 16 bits). But when I tried to read byte array, received from socket, I got some type anomaly. For example:
byte[] buffer =newbyte[1024];
DatagramPacket packet =new DatagramPacket(buffer, 1024);
socket.receive(packet);
byte[] data = packet.getData();
System.out.println("Received data:");
for (int i = 0; i < packet.getLength(); i++){
System.out.printf("%h:", data[i]);
}
I got such thing: 34:4:fffffff1:ffffffdb:ffffffdc:20:1:1:0:2:50
If I try to parse it using ByteArrayInputStream and DataInputStream
ByteArrayInputStream is =new ByteArrayInputStream(packet.getData());
DataInputStream dis =new DataInputStream(is);
int head = dis.readByte();
int recvinfo = dis.readByte();
short count = (short)dis.readShort();
System.err.printf("Decoded:\n head: %h\n recvinfo: %h\n count: %h\n",
head, recvinfo, count);
i got following:
Decoded:
head: 34
recvinfo: 4
count: fffff1db
So I get Integer entries in byte?! array. I also got Integer even I try to read and cast it as short in short!!! variable.
Please help!
P.S. Sorry for my bad English.

