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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:31:27+00:00 2026-06-17T02:31:27+00:00

I have recently been writing a Java NIO based server with non-blocking sockets and

  • 0

I have recently been writing a Java NIO based server with non-blocking sockets and I’ve bumped in to some problems in regarding writing the data out. I know by now that there are conditions where non-blocking write fails to write some, or all of the bytes in a ByteBuffer.

The way I currently handle such scenario is by either rewinding or compacting the buffer and later trying to send it again in next selection iteration. This how ever results in significant performance loss and it is imperative that I get the data sent fast.

I have attempted using something like:

ByteBuffer bb = ...;
SocketChannel sc = ...;
while(bb.remaining() > 0) { 
 sc.write(bb); 
} 

But the problem with this is the fact that it might write 0 bytes and still quit the while loop. I’m not sure why, but it seems like write() – method will hit ByteBuffer’s limit regardless of whether it actually sent all the bytes or not.

Another problem I’ve had with this writing method is when it sometimes under heavy load will cause a buffer overflow exception even when I’m not attempting a blocking write.

I desperately need some advice on how to properly perform blocking write and what condition might cause SocketChannel.write(ByteBuffer) to overflow the buffer(should it not stop when limit is hit?).

Thanks in advance.

Edit: I still have not found the reason why sc.write(bb) would set position in buffer to bb.limit() even if it wrote 0 bytes. My only resort remain to be rewinding the buffer after failed write attempt.

  • 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-17T02:31:28+00:00Added an answer on June 17, 2026 at 2:31 am

    When using non blocking IO you typically either after low latency or high throughput.

    Do not move data inside the buffer by compacting it, rather allocate buffers of the required lengths (to fit one message typically, message header separately). And dispose them after having fully written the contents to the socket. You can use pools of buffers of 2x growing sizes if you cannot accept GC.

    If throughput is your major concern then do not try writing to the socket directly, but rather register for socket writability with Selector and try writing when socket is writable. To achieve this you need to maintain a queue of buffers pending to be sent. Stay registered for the socket writability until this queue becomes empty. You should preferrably be using scatter/gather type IO in this case as it would minimize number of syscalls in the application.

    write requester thread:
      ioloop.submit(buffer[]{msgheaderbuf, msgbodybuf}, sock);
    selector thread (ioloop):
      submit(buffer[] bufs, socket sock):
        queue.enqueue(bufs);
        selector.register(sock, WRITABLE);
        selector.wakeup();
    

    If latency is important then first try to write to the socket directly and only if write fails enqueue the data and register for the socket writability as I described above. This will require additional mutex to protect the socket from being written both directly and from within the selector thread (as a result of writability event triggering).

    write requester thread:
      ioloop.submit(buffer[]{msgheaderbuf, msgbodybuf}, sock);
    selector thread (ioloop):
      submit(buffer[] bufs, socket sock):
        size = sock.write(bufs);
        while (!bufs.empty() && !bufs[0].remaining()): bufs.pop_front();
        if (bufs.empty()) return;
    
        queue.enqueue(bufs);
        selector.register(sock, WRITABLE);
        selector.wakeup();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently, I have been writing many classes which have, apart from generic variant, some
I have been recently writing a UDP server for a 2D shooter game I
Recently I have been writing some serializable object that also deals with specific logic
I have recently been working on implementing an ajax based file uploader for my
I have recently been rewriting some of my iphone app because a couple of
I have recently been writing git hooks for my project team. I would like
I'v been playing with java applets recently. In the one im writing now I
Recently I have been trying to update some code to utilise the standard C++
Recently, I have been battling with: weird table borders/margins, div alignments, positioning problems, and
Professionally I am a back-end Java developer, but recently I've been interested in writing

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.