Hi everyone,
I'm trying to do a PortScanner that really works, I've searched some tutorials about a PortScanner in java at google and I noted that the basic ideia that people use is:
1-Create a Socket object with host and port;
2-If the port is open you close the socket, else the IOException will catch it.
The big deal is that I tryed this code and it didnt works, it says that are ports opened in my machine when they werent opened. So I decide a different way as this:
But it's not working well too. If the host doesnt have a port open at 2020 it works fine, the method readInt() return me -1, but when the port is opened, the method readInt(); return nothing to me and my code get freeze because it's waiting for a response of the host.PHP Code:
try {
Socket clientSocket = new Socket("127.0.0.1", 2020);
clientSocket.setSoTimeout(3000);
DataInputStream inFromServer = new DataInputStream(clientSocket.getInputStream());
// Issue is here :(
System.out.println(""+inFromServer.readInt());
clientSocket.close();
} catch (SocketTimeoutException ex) {
System.out.println("Timeout exceed");
} catch (ConnectException ex) {
System.out.println("Port closed");
} catch (IOException e) {
System.out.println("Doesnt connect");
}
}
Does anybody know how can i fix this? Or an other better way to code a Port Scanner?
Thanks everybody!