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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:02:07+00:00 2026-05-30T07:02:07+00:00

I was messing around with NIO earlier today and as numClients got larger (~

  • 0

I was messing around with NIO earlier today and as numClients got larger (~2500 for my computer), I started getting the following exception:

java.net.ConnectException: Connection refused: no further information
    at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
    at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:701)
    at SocketInformationExceptionTest.run(SocketInformationExceptionTest.java:49)
    at java.lang.Thread.run(Thread.java:722)

From this code:

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class SocketInformationExceptionTest implements Runnable {
    private static interface Request {
        public void process(Selector sel);
    }

    private final Selector selector;
    private final BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

    public SocketInformationExceptionTest() throws IOException {
        selector = Selector.open();
    }

    public void addRequest(Request r) {
        requests.add(r);
        selector.wakeup();
    }

    @Override
    public void run() {
        while (true) {
            while (!requests.isEmpty()) {
                Request r = requests.poll();
                r.process(selector);
            }
            try {
                selector.select();

                Iterator<SelectionKey> itr = selector.selectedKeys().iterator();
                while (itr.hasNext()) {
                    SelectionKey key = itr.next();
                    itr.remove();

                    if (key.isValid()) {
                        if (key.isAcceptable()) {
                            ((ServerSocketChannel) key.channel()).accept();
                        } else if (key.isConnectable()) {
                            ((SocketChannel) key.channel()).finishConnect();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        int numClients = 2500;

        SocketInformationExceptionTest test = new SocketInformationExceptionTest();
        new Thread(test).start();

        final ServerSocketChannel server = ServerSocketChannel.open().bind(
                new InetSocketAddress(1234));
        server.configureBlocking(false);

        test.addRequest(new Request() {
            @Override
            public void process(Selector sel) {
                try {
                    server.register(sel, SelectionKey.OP_ACCEPT);
                } catch (ClosedChannelException e) {
                    e.printStackTrace();
                }
            }
        });

        for (int x = 0; x < numClients; x++) {
            final SocketChannel socket = SocketChannel.open();
            socket.configureBlocking(false);
            socket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
                    1234));

            test.addRequest(new Request() {
                @Override
                public void process(Selector sel) {
                    try {
                        socket.register(sel, SelectionKey.OP_CONNECT);
                    } catch (ClosedChannelException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        System.exit(0);
    }
}

Is this a case where my computer just can’t keep up with the requests or is something more sinister going on here? Googling turned up nothing of use.

Potentially relevant information:
java version “1.7.0_02”
Java(TM) SE Runtime Environment (build 1.7.0_02-b13)
Java HotSpot(TM) Client VM (build 22.0-b10, mixed mode, sharing)

32 bit Windows 7 Home Premium SP 1
AMD Turion M500 Dual Core 2.20 GHz
2.00 GB Memory
Realtek RTL8191SE Wireless LAN 802.11n PCI-E NIC

  • 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-30T07:02:09+00:00Added an answer on May 30, 2026 at 7:02 am

    You are opening thousands of client connections in a row with no intervening sleeps, so you are probably going to overflow the listen backlog queue of the ServerSocketChannel, at which point Windows starts refusing connections. Whatever you’re testing with this technique, it isn’t a valid test, as you are (a) starving the server thread of an opportunity to run and (b) probably about to exhaust the outbound port space, at which point you will start getting BindExceptions on the connect(), if you can get past your present issue.

    Also, you aren’t closing your connected and accepted channels, just accumulating them forever. So when you reach numClients = 2500 you have 5000 sockets open, do you realize that?

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

Sidebar

Related Questions

I am just messing around with reading input files with java until I got
I started messing around with Git today. I think I've done something wrong because
I've just started messing around with php recently and I was testing my new
Started messing around with Vimeo API in an attempt to create a clickable gallery
I've been messing around with javascriptmvc. After getting data from the model, the view
I've started messing around with Google App Engine, writing Python. I love Visual Studio
So I started messing around with gettext but I'm still puzzled about certain things,
I have been messing around with recursion today. Often a programming technique that is
When messing around with Python.h I got this error: AttributeError: 'module' object has no
Today I was messing around with malloc and integer pointer. The program is setup

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.