Jeffrey
Jeffrey

Reputation: 44798

Socket programming with NIO

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

Upvotes: 1

Views: 2770

Answers (1)

user207421
user207421

Reputation: 310850

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?

Upvotes: 3

Related Questions