0

作为一个更大的项目的切线,我正在尝试实现一些代码来扫描开放端口,以便我最终可以运行数据包测试。目前我只是想连接到 google.com 并且我知道端口 80 接受连接,所以我已经格式化了我的代码以扫描包括这个端口在内的一系列端口。但是,虽然我可以直接连接到端口 80,但扫描仪代码似乎通过了它。我想知道是否有人有任何想法?

public class PortScanner {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();
        String host = "www.google.com";

        System.out.println("Attempting to connect to " + host + " at port 80...");
        try {
            socket.connect(new InetSocketAddress(host, 80), 5000);
            System.out.println("Connection Successful.");
        }
        catch (Exception e) {
            //Just bypass the exceptions for now
        }

        System.out.println("Scanning for open ports on " + host);


        for (int i = 75; i < 85; i++) {
            System.out.println("Attempting to connect to " + host + " on port " + i);
            try {
                socket.connect(new InetSocketAddress(host, i), 5000);
                System.out.println("Open port found at " + i);
            }
            catch (ConnectException e) {
                System.err.println("Connect Exception Thrown on Port " + i);
                continue;
            }
            catch (SocketException e) {
                System.err.println("Socket Exception Thrown on Port " + i);
                continue;
            }
            catch (UnknownHostException e) {
                System.err.println("Unknown host at " + host);
                System.exit(1);
            }
        }

        System.out.println("Closing connection to " + host);
        socket.close();
        System.out.println("Connection closed");
    }
}

事实上,循环报告在端口 80 处抛出了一个 SocketException:

Attempting to connect to www.google.com at port 80...
Connection Successful.
Scanning for open ports on www.google.com
Socket Exception Thrown on Port 75
Socket Exception Thrown on Port 76
Socket Exception Thrown on Port 77
Socket Exception Thrown on Port 78
Socket Exception Thrown on Port 79
Socket Exception Thrown on Port 80
Socket Exception Thrown on Port 81
Socket Exception Thrown on Port 82
Socket Exception Thrown on Port 83
Attempting to connect to www.google.com on port 75
Socket Exception Thrown on Port 84
Attempting to connect to www.google.com on port 76
Attempting to connect to www.google.com on port 77
Attempting to connect to www.google.com on port 78
Attempting to connect to www.google.com on port 79
Attempting to connect to www.google.com on port 80
Attempting to connect to www.google.com on port 81
Attempting to connect to www.google.com on port 82
Attempting to connect to www.google.com on port 83
Attempting to connect to www.google.com on port 84
Closing connection to www.google.com
Connection closed

控制台日志按顺序出现的事实让我想知道连接尝试是否可能进入得太快,从而导致程序错过可用端口,但我不确定要做什么这样做。

任何想法表示赞赏!

4

1 回答 1

1

ASocket最多只能用于 1 个活动连接!而且由于您在端口扫描开始时连接(并且仅在最后关闭它),任何进一步的连接尝试(通过调用该connect()方法)都会抛出一个SocketException.

打印您的消息SocketException以验证这将是:"already connected"

Socket建议:每次尝试前创建一个新的。请注意, aSocket不能用于多个/新连接。引用 Javadoc 的Socket.close()

一旦套接字被关闭,它就不能用于进一步的网络使用(即不能重新连接或重新连接)。需要创建一个新的套接字。

于 2015-02-10T12:08:49.327 回答