How do I create auto-adjusting DatagramPacket?

  • Replies:0
Ben Hutchinson
  • Forum posts: 13

Apr 25, 2016, 1:16:04 PM via Website

I find that to receive data from DatagramSocket, I need a DatagramPacket, but to initialize a DatagramPacket object I need to pass it an initialized byte array, and the length of that byte array. Problem is such a byte array is of fixed size. However the datagrams my program is expecting to receive are not of a fixed size. They could be anywhere between 16 and 256 bytes in length.

How do I create a DatagramPacket with an auto-adjusting length, that I can then determine the length at the moment I receive the packet, so I can allocate the correct sized byte array based on this length? I don't want to be limited by being required to only send and receive fixed-length packets.

— modified on Apr 25, 2016, 1:17:13 PM

Reply