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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:23:17+00:00 2026-06-14T08:23:17+00:00

I want to transmit an int array over a network using Datagram Channel in

  • 0

I want to transmit an int array over a network using Datagram Channel in Java NIO API. However, the functions read/write can only take a ByteBuffer as an input. So I need to store the int data to a ByteBuffer and then read it back at the receiver. I am facing the java.nio.BufferUnderflowException. Here is what I do at the Sender Side:

for(i = 0; i < send_data.length; i++)
{
          //Write the data onto Buffer
          ByteBuffer buffer1 = ByteBuffer.allocate(send_data[i].length*4);
          for(j = 0; j < send_data[i].length; j++)
                     buffer1.putInt(send_data[i][j]);
          buffer1.flip();
          //Transmit the data
          while(buffer1.hasRemaining())
                 channel.write(buffer1);
}

Here, send_data is a 2D array where each row is treated as a separate data packet.

At the Receiver Side

for(i = 0; i < k; i++)
{
           ByteBuffer buffer = ByteBuffer.allocate((recvpkt[0].length)*4);
           channel.receive(buffer);
           j = 0;
           while(buffer.hasRemaining())
           {
                  recvpkt[i][j] = buffer.getInt();
                  j = j+1;
           }
 }

Similarly, recvpkt is a 2D array and each row of it will receive one packet from the network.

I read that getInt() reads 4 bytes from the ByteBuffer and moves the current position by 4 bytes. Is it that I am using the buffer.flip() in a wrong manner. I am new to using NIO and am having difficulty in how to go about debugging such issues.

UPDATE:

The error no longer occurs. But now all that the Receiver gets is all zeros. Whereas I am transmitting a binary sequence in each packet. When I read back from the buffer at the Sender side itself, all the entries turn up correctly but the buffer received at Receiver side has all zeros. No idea why this is happening.

UPDATE 2:

Finally, after hours of struggling, the problem is fixed. When you receive the buffer , you need to rewind it.

  channel.receive(buffer);
  buffer.rewind();

If you now use the getInt method on the buffer, you will be able to read the buffer successfully.

  • 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-14T08:23:18+00:00Added an answer on June 14, 2026 at 8:23 am

    You have th flip() the buffer each time you swap from write to read and from read to writing. Add flip() to the second example after receive()

    Once you have this working you should look at how you can re-use your ByteBuffers and EJP suggests.

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

Sidebar

Related Questions

I want to transmit data over the network, but I don't want to use
I want to transmit a serialized object over a socket channel. I want make
I want to transmit data from a Queue using Twisted. I currently use a
want to know why String behaves like value type while using ==. String s1
in my web application i want to transmit some values through the url between
I want to write a macro to write a string, using the compile-time optimisation
I am using a Tab based Activity in my App and I want to
How to implement java RMI over SSH v2 protocol.Is this possible?.If its not possible
I want to create a simplest remote desktop application using p2p communication. I did
I'm using C# and ASP.NET 2.5. I want a simple way to generate 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.