I am designing a port scanner to detect which ports are in use. The problem is it shows all ports as not in use, even though i know some of them are being used. Need help identifying the problem. Here's my code.
package portscanner;
import java.net.Socket;
import java.util.Scanner;
/**
*
* @author DeLL
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Enter the range of ports to be scanned:");
Scanner ob=new Scanner(System.in);
int start=ob.nextInt();
int stop=ob.nextInt();
if(start<0||stop<0||start>65535||stop>65535){
System.out.println("Invalid Range!!!");
}
for(int i=start;i<=stop;i++){
try{
Socket sock = new Socket("127.0.0.1",i);
System.out.println("Port in use :"+i);
sock.close();
}
catch(Exception e){
System.out.println("Port not in use: "+i);
}
}
}
}