开发者

Java NIO multiple socket connections failing

I have a program that attempts to connect to port 80 on different machines and reports if there is a server running. I am using NIO which therefore uses sockets to do the connection. I do a connect and then poll using finishConnect().

I am getting inconsistent behaviour. Sometimes the program correctly reports that there are web servers running on the various machines that I am scanning. However at other times the connections do not get reported even though there are webservers running on the target machines.

I would understand this if I was using UDP sockets as these are not reliable but I am using a TCP connection that should be reliable i.e no dropped packets.

I need to be able to scan many machines but this inconsistent behaviour exhibits it self even when testing the program with just 4 target IP addresses that all have webservers on port 80.

TIA

Rod

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 voi开发者_如何学Cd 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜