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 9232259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:16:18+00:00 2026-06-18T06:16:18+00:00

In Java a single char is 16 bit, 2 bytes. That means if I

  • 0

In Java a single char is 16 bit, 2 bytes. That means if I want to send an UDP packet to a server, I must find the length of the string (for example) and multiply by 2?

String string = "My awesome string!";
byte[] buff = new byte[ string.length*2 ];
buff = string.getBytes();
...
packet = new DatagramPacket(buff, buff.length, address, port);
socket.send(packet);

What about the UDP packet limit? 65k a packet. For example, if I want to send data files to a server I must send 65/2k data! I’m dividing 65 into 2, and what would be the buff limit? 65/2 or 65 kb?

For example:

byte[] buff = new byte[ 65000 ]

//file and bufferreader handle
while( ( line = bufferedReader.readLine() ) != null ){
   buff = line.getBytes();
   packet = new DatagramPacket(buff, buff.length, address, port);
   socket.send(packet);
}

I have read somewhere that I can send over than 65k data, because IPv4 protocol automatically divides the packet into pieces than the receiver will merge them. Is this true?

Why I’m getting white space into my buffer? I have written a client and a server app, and I’m using a 250 size buffer. If I’m sending a word, for example “Test”, which is 8 bytes long I’m getting a very long white space after the “Test” word:

byte[] buff = new byte[250];
packet = new DatagramPacket(buff, buff.length);
socket.receive(packet);
System.out.println("GET: " + buff);

and the console says:

GET: Test…………………………………………………………………

(dots represents white space)

  • 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-06-18T06:16:19+00:00Added an answer on June 18, 2026 at 6:16 am

    That means if I want to send an UDP packet to a server, I must find the length of the string (for example) and multiply by 2?

    No. String.getBytes() already allocates a proper array of the right length:

    byte[] buff = string.getBytes();
    

    Then, buff.length contains the proper length. Note that the length depends on the character set which was used to convert the String into the byte array.

    On a side note, you should avoid using the parameter-less String.getBytes() since it uses your platform’s default character set to convert the String into the byte array. Better pass the encoding explicitly:

    byte[] buff = null;
    try {
       buff = string.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
       e.printStackTrace();
    }
    

    I have read somewhere that I can send over than 65k data, because IPv4 protocol automatically divides the packet into pieces than the receiver will merge them. Is this true?

    It is true for TCP. I suggest that you use TCP, not UDP, in your case.

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

Sidebar

Related Questions

I have a single installation of java in a system that runs 2 or
Server (java): public static void main(String[] args) { //single threaded for now try {
I want to check if a single char is in a C string. The
Possible Duplicate: How to implement a single instance Java application? is there a way
I have a Java class which handles both single valued and multi-valued data. Therefore,
is there a way in Java to convert an Integer to single digits, and
How to convert hexadecimal string to single precision floating point in Java? For example,
In Java, you can define multiple top level classes in a single file, providing
Possible Duplicate: Is there a difference between single and double quotes in Java? I've
In Java there are a bunch of methods that all have to do with

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.