Java NIO多个套接字连接失败

时间:2011-01-13 14:20:28

标签: java sockets nio

我有一个程序尝试连接到不同计算机上的端口80,并报告是否有服务器正在运行。我正在使用NIO,因此使用套接字进行连接。我进行连接,然后使用finishConnect()进行轮询。

我的行为不一致。有时,程序会正确报告我正在扫描的各种计算机上运行的Web服务器。但是,在其他时候,即使在目标计算机上运行了Web服务器,也不会报告连接。

如果我使用UDP套接字,我会理解这一点,因为这些不可靠,但我使用的TCP连接应该是可靠的,即不丢弃数据包。

我需要能够扫描许多机器,但是这种不一致的行为即使在仅使用4个目标IP地址测试程序时也会显示出来,所有目标IP地址都在端口80上都有Web服务器。

TIA

class SiteFinder {

private static final long TIMEOUT = 500;

public void findSites() {

    int numSocketChannels = 100;
    int socketChannelCounter = 0;
    long ipAddressCounter = 0;
    boolean done = false;
    List<String> allIpAddresses =
            IPAddressGenerator.getIPAddresses(170);
    SocketChannel[] socketChannelArray =
            new SocketChannel[numSocketChannels];

    Iterator<String> itr = allIpAddresses.iterator();

    while(itr.hasNext()) {
        int k;
        for (k = 0; k < numSocketChannels && itr.hasNext(); k++) {
            String ipAddress = itr.next();
            ipAddressCounter++;
            if (ipAddressCounter % 50000 == 0)
                System.out.println(ipAddressCounter + " at " + new Date());
            try {
                socketChannelArray[k] = SocketChannel.open();
                socketChannelArray[k].configureBlocking(false);
                if (socketChannelArray[k].connect(
                            new InetSocketAddress(ipAddress,80))) {
                    System.out.println(
                            "connection established after connect() "
                            + ipAddress);
                    socketChannelArray[k].close();
                    socketChannelArray[k] = null;
                }
            } catch (IOException ioe) {
                System.out.println(
                        "error opening/connecting socket channel " + ioe);
                socketChannelArray[k] = null;
            }
        }

        while (k < numSocketChannels) {
            socketChannelArray[k++] = null;
        }

        long startTime = System.currentTimeMillis();
        long timeout = startTime + TIMEOUT;

connect:       
        while (System.currentTimeMillis() < timeout) {
            //System.out.println("passing");
            socketChannelCounter = 0;
            for(int j = 0; j < socketChannelArray.length; j++) {
                //System.out.println("calling finish connect");
                if (socketChannelArray[j] == null) {
                    ++socketChannelCounter;
                    if (socketChannelCounter == numSocketChannels) {
                        System.out.println("terminating connection loop");
                        break connect;
                    }
                    continue;
                }
                try {
                    if (socketChannelArray[j].finishConnect()) {
                        /*try {
                            out.write("connection established after " +
                            finishConnect()" +
                            clientChannelVector.elementAt(j).socket().
                            getInetAddress() + '\n');
                            out.flush();
                        } catch (IOException ioe) {
                            System.out.println(
                                "error writing to site-list "
                                + ioe.getMessage());
                        }*/
                        System.out.println(
                                "connection established after finishConnect()"
                                + socketChannelArray[j].socket().
                                getInetAddress());
                        socketChannelArray[j].close();
                        socketChannelArray[j] = null;
                    }
                } catch (IOException ioe) {
                    System.out.println(
                            "error connecting from "
                            + "clientChannel.finishConnect()");
                    try {
                        socketChannelArray[j].close();
                    } catch (IOException e) {
                        System.out.println("error closing socket channel");
                    } finally {
                        //System.out.println("removing socket channel");
                        //System.out.println(clientChannelVector.size());
                        socketChannelArray[j] = null;
                    }
                }
            }
        }
        closeConnections(socketChannelArray);
    }
}

private void closeConnections(SocketChannel[] socketChannelArray) {
    for (int i = 0; i < socketChannelArray.length; i++) {
        if (socketChannelArray[i] == null) {
            continue;
        }
        try {
            socketChannelArray[i].close();
            //System.out.println(
                //"TIME OUT WAITING FOR RESPONSE CLOSING CONNECTION");
        } catch (IOException ioe) {
            System.out.println(
                    "error closing socket channel " + ioe.getMessage());
        }
    }
}

}

0 个答案:

没有答案