DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2005
    Posts
    3

    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[256];
    // receive request
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    incoming.receive(packet);
    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 08:31 PM.

  2. #2
    Join Date
    Dec 2003
    Location
    tx/us
    Posts
    131
    Have you tried using getLength() on the incoming packet to limit the number of bytes copied out to the String?

  3. #3
    Join Date
    May 2005
    Posts
    3
    Thanks a lot, that worked. I'm very new to networking in java.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links