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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:32:42+00:00 2026-05-27T07:32:42+00:00

This came up while answering BufferedWriter only works the first time As far as

  • 0

This came up while answering BufferedWriter only works the first time

As far as I understand the Java Doc (and this is confirmed by many posts on the net) a DatagramPacket should not accept more data than it’s current size. The documentation for DatagramSocket.receive says

This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received message. If the message is longer than the packet’s length, the message is truncated.

So, I made a program which reuses the receiving packet and send it longer and longer messages.

public class ReusePacket {

    private static class Sender implements Runnable {

        public void run() {
            try {
                DatagramSocket clientSocket = new DatagramSocket();
                byte[] buffer = "1234567890abcdefghijklmnopqrstuvwxyz".getBytes("US-ASCII");
                InetAddress address = InetAddress.getByName("127.0.0.1");

                for (int i = 1; i < buffer.length; i++) {
                    DatagramPacket mypacket = new DatagramPacket(buffer, i, address, 40000);
                    clientSocket.send(mypacket);
                    Thread.sleep(200);
                }                  
                System.exit(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String args[]) throws Exception {
        DatagramSocket serverSock = new DatagramSocket(40000);
        byte[] buffer = new byte[100];
        DatagramPacket recievedPacket = new DatagramPacket(buffer, buffer.length);

        new Thread(new Sender()).start();

        while (true) {
            serverSock.receive(recievedPacket);
            String byteToString = new String(recievedPacket.getData(), 0, recievedPacket.getLength(), "US-ASCII");
            System.err.println("Length " + recievedPacket.getLength() + " data " + byteToString);
        }
    }
}

The output is

Length 1 data 1
Length 2 data 12
Length 3 data 123
Length 4 data 1234
Length 5 data 12345
Length 6 data 123456
...

So, even if the length is 1, in for the next receive it gets a message with length 2 and will not truncate it. However, if I manually set the length of the package then the message will be truncated to this length.

I have tested this on OSX 10.7.2 (Java 1.6.0_29) and Solaris 10 (Java 1.6.0_21). So to my questions.

Why does my code work and can expect it to work on other systems also?

To clarify, the behavior seems to have changed sometime in the past (at least for some JVMs), but I don’t know if the old behavior was a bug. Am I lucky it works this way and should I expect it to work the same way on Oracle JVM, IBM JVM, JRockit, Android, AIX etc?

After further investigation and checking the source for 1.3.0, 1.3.1 and 1.4.0 the change was introduces in Sun implementation from 1.4.0, however, there is no mention of this in either the release notes or the network specific release notes of JDK 1.4.0.

  • 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-27T07:32:43+00:00Added an answer on May 27, 2026 at 7:32 am

    There are two different lengths here. The length of the packet is set to 100 in the constructor:

    DatagramPacket recievedPacket = new DatagramPacket(buffer, buffer.length);
    

    According to the docs, the length() method tells you the length of the message currently stored in the packet, which it does. Changing

    byte[] buffer = new byte[100];
    

    to

    byte[] buffer = new byte[10];
    

    yeilds the following output:

    Length 1 data 1
    Length 2 data 12
    ...
    Length 9 data 123456789
    Length 10 data 1234567890
    Length 10 data 1234567890
    Length 10 data 1234567890
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Kicking around some small structures while answering this post , I came across the
I came across this presentation while browsing SO some time ago, and it relates
I am confused about StringPool in Java. I came across this while reading the
I came across this control while using a webapp (this in invite-only beta) and
While trying to understand the MVVM pattern I came across this video linked from
While answering this question I came across an interesting difference in the overload resolution
I came across this class while reading a C# book and have some questions.
I came across this problem while preparing for an interview and curious to know
While browsing I came across this blog post about using the Wikipedia API from
While doing some small regex task I came upon this problem. I have a

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.