I’m developing a chat application in java using DatagramSocket and DatagramPacket .
The data packet received is in terms of bytes .
but i dont Know how to chop of unused bytes
Here is the Code:-
String s,pack[];
byte[] data=new byte[100];
data=dp.getData();//dp is instance of DatagramPacket
s=new String(data)
pack=s.split("-");//Username-ConnectionProtocol-IPaddress
In this code last split gets ip addresss along with the unused data in bytes nearly .
so i need a solution to chop off the unused data in bytes .
like 127.0.0.1[][][][][][][][][]…..
i tried to chop string Pack using subtstring() but it will vary in length.
so i need some help in this problem.
new byte[100], becausedp.getData()discards the 100 bytes that you allocated.For example, if you know that unused bytes are always at the end of the packet, and that they are always set to zero, you could use this piece of code to set up your string: