Trying to convert the resulting DatagramPacket to a string, but I have a little problem. Not sure if this is the best way.
The data that I receive is basically of unknown length, so I have some buffer [1024] installed on my receiving side. The problem is that I sent the string "abc" and will do the following on my side of the recipient ...
buffer = new byte[1024]; packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); buffer = packet.getData(); System.out.println("Received: "+new String(buffer));
I get the following output: abc [] [] [] []] [] [] [] ..... up to the length of the buffer. I assume that all junk / nulls at the end should be ignored, so I have to do something wrong. "I know that the buffer.length problem is a problem, because if I change it to 3 (for this example), my come out just fine.
Thanks.
source share