Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7700185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:39:00+00:00 2026-05-31T22:39:00+00:00

Hi I have code which sets up a basic client to server scenario and

  • 0

Hi I have code which sets up a basic client to server scenario and exchanges data between them. Basically the server waits for data from the client and, once sent by the client, prints it to the screen.

I have been tasked with getting the length of the data sent. I am a-little confused as to what this means as it could mean the actual length of the data or the total size of the packet sent.

The message is simply Mary.
Now does this mean the packet size would be 4? I have code that gets this like so:

System.out.println( "Packet Length: "+packet.getData().length );
System.out.println( "Data Length: "+message.getBytes().length );

They both return 4. But the maximum data packet size is 65535 so Im wondering if this number should be a lot bigger…

Now with code:

 public static void main(String[] args) throws IOException 
{
    String message = "Mary";
    byte[] buf = message.getBytes();
    System.out.println("Aclient: message is:" + message);

    String serverName = "127.0.0.1";
    if (args.length == 1) {
        serverName = args[0];
    }
    // get a datagram socket
    DatagramSocket socket = new DatagramSocket();

    // send request        
    InetAddress address = InetAddress.getByName(serverName);
    int port = 4441;
    DatagramPacket packet = new DatagramPacket(
                            buf, buf.length, address, port);

    System.out.println( "Packet Length: "+packet.getData().length );
    System.out.println( "Data Length: "+message.getBytes().length );


    System.out.println("Aclient: destination name:" + 
            address.getHostName() + "  destination port:" + port);

    System.out.println("Aclient: sending datagram");
    socket.send(packet);    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:39:02+00:00Added an answer on May 31, 2026 at 10:39 pm

    What you see is the expected behavior. "Mary".getBytes() returns a byte[] with length 4; that’s 1 byte for each character, which makes sense because it’s nothing but ASCII.

    Likewise, you’ve told the DatagramPacket that you’re sending those same 4 bytes, so it should be no surprise that DatagramPacket#getData() agrees with …the data you’re asking it to send.

    Hopefully it makes sense to you that the packet wouldn’t use the full 65535 bytes unless you actually have that much data to send. It’s a maximum, not a minimum, size.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code which selects data from my database and then sets these selected
I have code which as been working against an older Active Directory server and
I have code which performs query on Jboss server. It has JNDI based datasource
I have some code which collects points (consed integers) from a loop which looks
i have an example of a source code which programatically sets the value of
I have the following code which sets up some custom form elements on the
I have code which needs to do something like this There is a list
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
I have code which works in all sorts of different situations, including when copying
I already have code which lazy loads scripts on request. My issue now is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.