Network Packet Interpretation problem
Send a 3 character packet, Client Code Snip:
String alias = "Bob";
byte buf = alias.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 4445);
Server Code Snip:
byte buf = new byte;
// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
String received = new String(packet.getData());
The problem is that received (String) ends up containing "Bob" plus many characters of blank space. I just want valid characters in String received, which would be just "Bob" in this case. Should I add a character to indicate the end of the String? I'm hoping there is an easier, more efficient way.
Last edited by pyromania; 05-24-2005 at 09:31 PM.
Have you tried using getLength() on the incoming packet to limit the number of bytes copied out to the String?
Thanks a lot, that worked. I'm very new to networking in java.
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center