Domanda

I am writing a port scanner in Java. I am using a for loop to count through ports from 0 to 100, constructing a socket on each and seeing if it throws an exception or not. However, when I try to scan non local ports such as on "www.google.com" It scans the first one and then stops (literally the program just waits), if anyone knows why this is I would like to know. Here is the code for the Scan Method. Please note the variable ia is an InetAddress object

private void scan(int low, int high){
    for(int port =low; port<high; port++){
    try {
    connection = new Socket(ia, port);
    r.addPort(port);
    System.out.println("port "+port+" open");
    connection.close();
    } catch (IOException e) {
    System.out.println("port "+port+" not open");
    }
}
}
È stato utile?

Soluzione

try to set a timeout on a socket. It will fail faster.

connection.setSoTimeout(10*1000); // milliseconds

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top