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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:46:19+00:00 2026-06-05T08:46:19+00:00

I’ve built a Selector-based system in Java that can accept multiple clients. It has

  • 0

I’ve built a Selector-based system in Java that can accept multiple clients. It has a ServerSocketChannel registered under OP_ACCEPT, which accept()s the incoming connection and registers the resulting SocketChannel with the selector again. Here’s that bit:

ServerSocketChannel insock = ServerSocketChannel.open();
    insock.configureBlocking(false);
    insock.socket().bind(new InetSocketAddress(6789));

    Selector sel = Selector.open();
    SelectionKey joinchannel = insock.register(sel, SelectionKey.OP_ACCEPT);

    System.out.println("Ready to accept incoming connections.");

    while (true) {
        int ready = sel.selectNow();
        if (ready == 0)
            continue;
        Set<SelectionKey> selectedKeys = sel.selectedKeys();
        Iterator<SelectionKey> keyIterator = selectedKeys.iterator();
        while (keyIterator.hasNext()) {
            SelectionKey key = keyIterator.next();
            if(key.isAcceptable()){
                SocketChannel newConnection = insock.accept();
                System.out.println("New client "+newConnection+" connected.");
                newConnection.configureBlocking(false);
                newConnection.register(sel, SelectionKey.OP_READ).attach(new DGPlayer());
            }

If I register the new SocketChannel for OP_READ, this works fine. The check for isReadable() succeeds, reads data. Here’s that bit:

else if(key.isReadable()){
                ByteBuffer buf = ByteBuffer.allocate(1024);
                int trans = ((SocketChannel)key.channel()).read(buf); buf.flip();
                byte[] ba = new byte[buf.remaining()]; buf.get(ba);
                String msg = new String(ba, 0, ba.length, Charset.forName("UTF-8"));

                if(trans > 0){
                    DGPlayer client = (DGPlayer) key.attachment();
                    System.out.println(client.name+": "+msg.trim());
                }
            }
                //              else if(key.isWritable()){
//                  ByteBuffer buf = ByteBuffer.allocate(48);
//                  buf.clear();
//                  buf.put(((String)key.attachment()).getBytes());
//                  buf.flip();
//                  
//                  SocketChannel target = ((SocketChannel)key.channel());
//                  while(buf.hasRemaining()) {
//                      target.write(buf);
//                  }
//                  
//                  key.attach(null);
//              }

If I register the SocketChannel for OP_READ|OP_WRITE, however, nothing happens. As far as I can tell, the selector never finds the channel to be either readable() or writable(). The only change is registering the channel for WRITE permission.

Any idea why this might be?

EDIT – For completion’s sake, here’s some client code:

while (true) {
        int readyChannels = sel.select();
        if (readyChannels == 0)
            continue;

        Set<SelectionKey> selectedKeys = sel.selectedKeys();
        Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

        while (keyIterator.hasNext()) {
            SelectionKey key = keyIterator.next();

            if (key.isReadable()) {
                ByteBuffer buf = ByteBuffer.allocate(1024);
                int trans = ((SocketChannel)key.channel()).read(buf); buf.flip();
                byte[] ba = new byte[buf.remaining()]; buf.get(ba);
                String msg = new String(ba, 0, ba.length, Charset.forName("UTF-8"));

                if(msg.trim().length() != 0)
                    System.out.println("Response from server: "+msg.trim());
            }
            else if(key.isWritable() && messages.size() > 0){
                String message = messages.remove();
                ByteBuffer buf = ByteBuffer.allocate(48);
                buf.clear();
                buf.put(message.getBytes(Charset.forName("UTF-8")));

                buf.flip();

                int written = outsock.write(buf);
                while(buf.hasRemaining()){
                    outsock.write(buf);
                }

                System.out.println("Wrote '"+message+"' to server");
            }
            keyIterator.remove();
        }
    }

(The Client registers the Server for OP_READ|OP_WRITE)

  • 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-05T08:46:21+00:00Added an answer on June 5, 2026 at 8:46 am

    In general it’s a bad idea to have a channel registered for OP_WRITE if you don’t have anything to actively write to it. Most channels can be written to most of the time, so every call to Selector.select will return without blocking and consume resources. It’s better to add the writing flag to SelectionKey.interestOps when you’re ready to write something to the channel, and remove the flag when you’re done.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

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.