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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:20:09+00:00 2026-05-18T04:20:09+00:00

I’ve just started using java.nio and probably used it some wrong way so I

  • 0

I’ve just started using java.nio and probably used it some wrong way so I got a slight problem with it.

I’m trying to write a something like a Port Forwarder that can modify the traffic that passes through it using various additional modules.

Here is how I’m doing it:

  1. ConnectionManager – a thread that
    has it’s own Selector that is
    registered to OP_ACCEPT on
    ServerSocketChannel. Whenever it
    selects anything – it creates a
    ConnectionProcessor object that
    manages the connection.

  2. ConnectionProcessor – a thread
    that opens SocketChannel to
    predefined forward point (where to
    send the packets from the newly
    connected client). Then it opens
    it’s own Selector and registers it
    to client’s SocketChannel’s OP_READ
    and server’s SocketChannel’s
    OP_READ.

Then the processor goes into infinite loop selecting data from selector and forwarding it appropriately. To determine where to send data it compares SelectionKey.channel() to clientChannel and serverChannel.

Selection in ConnectionProcessor is made with timeout of 5 seconds (select(5000)) – to handle timeouts. When select times out – it tries to read from both channels to get an exception or -1 result.

Now here are my questions/problems:

  1. Is it right to use key.cancel()
    after processing the key? Most
    examples I’ve seen in the internet
    simply remove the key from
    selectedKeys() list. key.cancel()
    seems to be much better approach.
  2. Is it right to have several
    selectors that basically use the
    same ServerSocketChannel? Or should I always use single Selector and pass selected keys
    to appropriate Managers? What I
    mean is that if 3 clients connect
    simultaneously then this is what
    will happen:

    a) Manager creates Processor.
    Processor opens client channel.
    Processor registers it’s own
    selector to the client channel. b)
    repetition of (a) c) repetition of
    (a)

  3. For some reason, after even one client connects to my forwarder – it won’t process
    messages faster than 5000msec timeout. It starts selecting, locks for 5 seconds, then go
    to second iteration and fetches me 5-6 messages that I received during previous timeout.
    Should I blame (1), (2), or some other reason?
  4. Is there any manual on how this all nio stuff works internally? I’m kind of a person that understands how to use things only after I fully understand the mechanics underneath. Reading API does not help as it is written for people who already know the proper way of using nio.

Thanks for reading my whole question and thanks in advance for any help.

  • 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-18T04:20:09+00:00Added an answer on May 18, 2026 at 4:20 am
    1. No. Just remove the key from the selected set. Usually this is done via iterator.remove(). If you cancel they key it will never be selected again.

    2. It’s pointless. You don’t need the second selector, or the extra thread either. That’s what NIO is for. You can handle it all with the original selector within the original thread.

    3. It’s probably caused by strange code. Redo it as above and see if it still happens. If so, post some code here.

    4. You would need to read the Berkeley Sockets API or a good book such as Stevens, Unix Network Programming, or mine 😉

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

Sidebar

Related Questions

No related questions found

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.