DatagramPacket to string
Asked Answered
P

6

6

Trying to convert a received DatagramPacket to string, but I have a small problem. Not sure what's the best way to go about it.

The data I'll be receiving is mostly of unknown length, hence I have some buffer[1024] set on my receiving side. The problem is, suppose I sent string "abc" and the do the following on my receiver side...

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[][][][]][][][]..... all the way to the buffer length. I'm guessing all the junk/null at the end should've been ignored, so I must be doing something wrong." I know the buffer.length is the problem because if I change it to 3 (for this example), my out comes out just fine.

Thanks.

Physiological answered 19/12, 2011 at 4:35 Comment(0)
D
10
new String(buffer, 0, packet.getLength())
Dessiedessma answered 19/12, 2011 at 4:41 Comment(2)
Unfortunately that didn't work. The out became completely nonsensical. And also eclipse warns that the method String(byte[], int) is deprecated. When I initialize he buffer with the exact length of string to be received, everything works great, but still stuck with the same issue. The length of the string will not be known in advance.Physiological
This did work however new String(buffer, 0, packet.getLength()); Thanks a mil.Physiological
B
5

Using this code instead: String msg = new String(packet.getData(), packet.getOffset(), packet.getLength());

Bewitch answered 7/1, 2017 at 5:42 Comment(0)
G
2

The DatagramPacket's length field gives the length of the actual packet received. Refer to the javadoc for DatagramPacket.receive for more details.

So you simply need to use a different String constructor, passing the byte array and the actual received byte count.

See @jtahlborn or @GiangPhanThanhGiang's answers for example.


However, that still leaves the problem of which character encoding should be used when decoding the bytes into a UTF-16 string. For your particular example it probably doesn't matter. But it you are passing data that could include non-ASCII characters, then you need to decode using the correct charset. If you get that wrong, you are liable to get garbled characters in your String values.

Givens answered 19/12, 2011 at 4:41 Comment(0)
C
0

As I understand it, the DatagramPacket just has a bunch of junk at the end. As Stephen C. suggests, you might be able to find the actual length received. In that case, use:

int realSize = packet.getLength() //Method suggested by Stephen C.
byte[] realPacket = new byte[realSize];
System.arrayCopy(buffer, 0, realPacket, 0, realSize);

As for finding the length, I don't know.

Crackleware answered 19/12, 2011 at 6:7 Comment(5)
If you're going to downvote, please explain why so that I can fix my posts for next time. Simply downvoting does not help.Crackleware
you original answer did not provide any meaningful information about getting the packet length, which was basically what the question was about. also, once you know the packet length, there is no need to copy the array.Dessiedessma
The copying is to reduce the buffer size from 1024 to the packet size.Crackleware
The copying is unnecessary once you know the actual packet size. For example, `new String(packet.getData(), packet.getOffset(), packet.getLength());Hicks
And 'as for finding the length', you've already found it. Answer is totally unclear.Hicks
D
0

Try

System.out.println("Received: "+new String(buffer).trim());

or

String sentence = new String(packet.getData()).trim();
System.out.println("Received: "+sentence);
Displease answered 17/12, 2017 at 20:26 Comment(0)
E
-2

Use this Code instead

buffer = new byte[1024]; 
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
String data = new String(packet.getData());
System.out.println("Received: "+data);
Escharotic answered 2/11, 2016 at 19:42 Comment(1)
Use this code why? Mere code is not an answer. You have to explain. This particular code doesn't work any better than the OP's original code, as it is semantically identical.Hicks

© 2022 - 2024 — McMap. All rights reserved.