Hey everyone,
I am using this code to receive data from a UDP socket and return it as string:
byte[] receiveData = new byte[MAX_PACKET_SIZE];
DatagramPacket receivedPacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivedPacket);
return new String(receivedPacket.getData(), 0, receivedPacket.getLength());
I set MAX_PACKET_SIZE to 1024 which causes Java to reserve a 1024bit long byte[] everytime a packet arrives. This causes a lot of overhead if the packet is much smaller than 1024 byte.
Is there a better way to accomplish this? Maybe with some kind of while() read cycle? I am not very familiar with Java yet. :)
Thank you very much!