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

  • SEARCH
  • Home
  • 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 7999223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:26:35+00:00 2026-06-04T15:26:35+00:00

I am implementing a p2p application, in which nodes communicate by UDP packet. The

  • 0

I am implementing a p2p application, in which nodes communicate by UDP packet. The packets read from InputStream are sometime not complete.

Here is my code:

…

protected String key;
    protected Identifier messId; //Identifier hold a BigInteger
    protected String range;
    protected String concat;

….

public ReplicationMessage(DataInput in) throws IOException {
       fromStream(in);
}


public void fromStream(DataInput in)
    try {
    super.fromStream(in);      

    int length=in.readInt();
    byte[] data=new byte[length];
    in.readFully(data);
    concat = new String(data);
    System.out.println("concat: "+concat); 

            messId = new Identifier(in);

    } catch (IOException e) {
        e.printStackTrace();
    }        
  }

public void toStream(DataOutput out) {
    try {

    super.toStream(out);

    byte[] data = concat.getBytes();
    out.writeInt(data.length);
    out.write(data);        

    messId.toStream(out);

    } catch (IOException e) {

e.printStackTrace();
    }   
}

the read packet sometime is complete, for example

concat: 179136678282544:140737488355328

but sometime is not complete, for example

concat: 179136678282544
concat: 179136678282544
concat: 179136678282544

Can any one tell me what the problem is?

Many thanks

Here are the code for sending/receiving the UDP packet
for sending:

private void sendMessage(int comm, Message message, InetAddress ip, int port)
throws IOException {

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    DataOutputStream dout = new DataOutputStream(bout);

    dout.writeInt(comm);

    dout.writeByte(message.code());

    message.toStream(dout);

    dout.close();

    byte[] data = bout.toByteArray();
    if (data.length > DATAGRAM_BUFFER_SIZE) {
        throw new IOException("Message too big, size="+data.length+
                              " bytes, max="+DATAGRAM_BUFFER_SIZE+" bytes");
    }

    DatagramPacket packet = new DatagramPacket(data, data.length, ip, port);

    socket.send(packet);
}

For receiving the UDP packet

     byte[] buffer = new byte[DATAGRAM_BUFFER_SIZE];

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

     socket.receive(packet);

The sending node and receiving node belonging to one machine. the Buffer receiving packet is set to 10*1024 bytes which is much larger than packet length

Here is code for turning incoming datapacket into stream

                ByteArrayInputStream bin = new ByteArrayInputStream(packet.getData(),
                                             packet.getOffset(), packet.getLength());

                DataInputStream din = new DataInputStream(bin);

                int comm = din.readInt();

                byte messCode = din.readByte();

                Message message = factory.createMessage(messCode, din);
  • 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-04T15:26:37+00:00Added an answer on June 4, 2026 at 3:26 pm

    UDP does not deliver damaged or partial packets. Either you aren’t sending all the data or you aren’t unpacking it properly. You still haven’t posted all the code concerned so it is not possible to say exactly where.

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

Sidebar

Related Questions

Hi I am implementing P2P chatting application where server will mediator for exchangeing IP
I have the work of implementing a distributed system of nodes (like p2p nodes)
When implementing the Strategy Pattern, where does one put the code that determines which
While implementing XML file reading/writing in my application I saw that when I call
When implementing the ISerializable interface in C#, we provide a constructor which takes a
When implementing a Matrix construct using arrays, which would be more efficient? Using a
Whilst implementing my first MVVM application in WPF, I've been wondering about the pros
We are designing a p2p applications using c++ which transmits voice to other peer
Implementing agile in projects requires the ability to do refactoring. It is not really
I implementing OLE DB provider for my custom database. It will be used from

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.