Port Scanner

Port scanner is a application which scans for open port on a system. Concept for port scanner is to try connection with every port on target machine from 0 to 65535.



We will write a simple Socket client program which will try to connect target on all port number. In case if port number is open connection will be established otherwise exception will be thrown.

/*****************************************
**      Port Scanner In Java 
**      www.jgyan.com
******************************************/			
import java.net.*;
class portScanner 
 {
   public static void main(String arg[])
    {
	   int port=0;
	   Socket sc;
	   if(arg.length==0)
	    System.out.println("Usage: \n java portScanner target-ip");
	   else	
	    for(;port<65536;port++)
		 {
		 try 
	      {
		  sc= new Socket(arg[0],port);
		  System.out.println(port+":Open");
		  }
		catch(Exception e)
          {}			  
		 }
	}
}