Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NIO多套接字连接失败_Java_Sockets_Nio - Fatal编程技术网

Java NIO多套接字连接失败

Java NIO多套接字连接失败,java,sockets,nio,Java,Sockets,Nio,我有一个程序,尝试连接到不同机器上的端口80,并报告是否有服务器在运行。我使用的是NIO,因此它使用套接字进行连接。我进行连接,然后使用finishConnect进行轮询 我的行为越来越不一致。有时程序会正确地报告我正在扫描的各种机器上运行着web服务器。但是,在其他情况下,即使目标计算机上运行着Web服务器,也不会报告连接 如果我使用UDP套接字,我会理解这一点,因为它们不可靠,但我使用的TCP连接应该是可靠的,即没有丢弃的数据包 我需要能够扫描多台机器,但这种不一致的行为表现出它自己,即使在

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

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

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

我需要能够扫描多台机器,但这种不一致的行为表现出它自己,即使在测试只有4个目标IP地址的程序时,所有这些地址都在端口80上有Web服务器

短暂性脑缺血发作


}

您说您已连接,然后使用finishConnect进行投票。您是如何轮询的,即检查一次并放弃,每n毫秒轮询一次,持续n秒?你有可以分享的代码吗?这可能有助于理解可能的原因。您为什么不使用选择器?您似乎正在重新设计它的一些功能。您是否有任何理由不使用新的URLhttp://+host+/.openStream;跨多个线程。您只需几行代码就可以实现大致相同的效果。感谢您的回复。我来看看选择器和URL类。
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());
        }
    }
}