1

My client code like this :

   socket = new Socket("10.105.1.20", 5678);
   DataOutputStrean dos = new DataOutputStream(socket.getOutputStream());
   short len = 4;
   dos.writeByte(len);
   dos.writeByte(len >> 8);
   dos.writeShort(0x0410);
   dos.flush();

It should be 4 bytes send to the server.But when I use wireshark fetch the packet,the data is 3 bytes

   00 04 10

I think it should be

   04 00 04 10

Where's the first byte 04 ? Is there sth wrong in my code or it is related to the wireshark.THX for your helps!

Leo Xu
  • 174
  • 3
  • 11

2 Answers2

4

There is nothing that guarantees that all four writes will be sent in the same packet. Probably the first byte was sent by itself,then the next three were coalesced by the Nagle algorithm. You could use a BufferedOutputStream and flush it when you want the writes to be sent, but there is still no guarantee. TCP can segment your data any way it likes.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • yeah,I find the first byte in the prev packet.Each message sent in my client and server will be divided into 2 segment(1 byte + the next bytes) – Leo Xu Nov 06 '13 at 01:16
-1

The short primitive data type is 16-bits in Java.

This SO answer shows how to convert a short to a byte[], that might be what you are looking for.

Additionally this SO question has more information on byte ordering.

Community
  • 1
  • 1
Dave G
  • 9,639
  • 36
  • 41