DatagramPacket to string

前端 未结 6 1182
南旧
南旧 2020-12-11 05:18

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 most

6条回答
  •  长情又很酷
    2020-12-11 05:57

    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.

提交回复
热议问题