使用NIO进行套接字编程

时间:2012-02-23 02:47:01

标签: java sockets nio

我今天早些时候正在弄乱NIO,随着numClients变大(我的电脑为{2500),我开始遇到以下异常:

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)

从这段代码:

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);
    }
}

这是一个我的电脑无法跟上请求的情况还是在这里更加险恶的事情?谷歌搜索没有任何用处。

潜在相关信息:
java版“1.7.0_02”
Java(TM)SE运行时环境(版本1.7.0_02-b13)
Java HotSpot(TM)客户端VM(版本22.0-b10,混合模式,共享)

32位Windows 7 Home Premium SP 1
AMD炫龙M500双核2.20 GHz
2.00 GB内存
Realtek RTL8191SE无线LAN 802.11n PCI-E网卡

1 个答案:

答案 0 :(得分:3)

您连续打开数千个客户端连接而没有中间休眠,因此您可能会溢出ServerSocketChannel的侦听backlog队列,此时Windows开始拒绝连接。无论你使用这种技术测试什么,它都不是一个有效的测试,因为你是(a)使服务器线程缺乏运行机会和(b)可能即将耗尽出站端口空间,此时你将如果你能解决现在的问题,就开始在connect()上获取BindExceptions。

此外,您没有关闭已连接和已接受的频道,只是永久累积它们。所以当你到达numClients = 2500时,你有5000个套接字打开,你意识到了吗?