Writing a port scanner in Java - Page 2
Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Writing a port scanner in Java

  1. #11
    Junior Member
    Join Date
    Feb 2005
    Posts
    1
    I consider myself a complete noob when it comes to java.....
    A couple minutes before finding this, I wrote this....doesn't it do the same thing?

    Code:
    import java.io.*;
    import java.net.*;
    
    public class PortScan {
    	
    	public static void main(String[] args) throws IOException {
    		
    		Socket j = new Socket();
    		BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    		BufferedWriter outFile = new BufferedWriter(new FileWriter(new File("PortScan.txt")));
    		
    		System.out.println("IP Address or Computer Name?");
    		String compname = input.readLine();
    		System.out.print("\nFrom: ");
    		String x1 = input.readLine();
    		int start = 0; int finish = 0;
    		System.out.print("\nTo: ");
    		String x2 = input.readLine();
    		
    		try {
    			start = (Integer.parseInt(x1));
    			finish = (Integer.parseInt(x2));
    		}
    		catch(Exception gay1) {
    			System.out.println("Invalid Ports");
    			System.exit(1);
    		}
    		
    		System.out.println("Scanning...");
    		for(int x = start; x <= finish; x++) {
    			System.out.println("Checking Port " + x);
    			outFile.write("Port " + x + ":");
    			try {
    				j = new Socket(compname,x);
    				System.out.println("Port Open");
    				outFile.write(" Open");
    				outFile.newLine();
    			}
    			catch(Exception gay2) {
    				System.out.println("Port Closed");
    				outFile.write(" Closed");
    				outFile.newLine();
    			}
    		}
    		outFile.close();
    		System.out.println("\nScan Complete");
    	}
    }

  2. #12
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    Yes, it does do the same thing. However, I'd recommend closing each Socket object like so...
    Code:
    		for(int x = start; x &lt;= finish; x++) {
    			System.out.println("Checking Port " + x);
    			outFile.write("Port " + x + ":");
    			try {
    				j = new Socket(compname,x);
    				System.out.println("Port Open");
    				outFile.write(" Open");
    				outFile.newLine();
                                     j.close()
    			}
    			catch(Exception gay2) {
    				System.out.println("Port Closed");
    				outFile.write(" Closed");
    				outFile.newLine();
    			}
    		}
    Otherwise, the sockets will remain open until the program closes.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

  3. #13
    Member
    Join Date
    Oct 2004
    Posts
    40
    Just out of curiousity, how is the resource efficiency of this system? Java tends to be a resource whore, which is a small part of the reason I don't care for it. Also I noticed a comment about raw sockets, well, that's impossible in java, simply because of the platform independence that has become the hallmark of java. This, because of the JVM, makes it impossible to do something of this nature. To do that would totally remove the platform independent nature of java, making its inefficiency merely a hindrance.
    --BigDick


    \"When in Rome, eat Rome!\" -Godzilla

  4. #14
    Antionline Herpetologist
    Join Date
    Aug 2001
    Posts
    1,165
    I don't know how much memory an instance of this class takes, but since each socket is opened only when the previous one is closed, it's not very memory hungry. I just ran it to check, and it takes a little over 8 megs and almost no CPU when running on my Windows XP system. Also, about the raw socket support. If the person running it had jpcap and winpcap/libpcap installed on the system running it, raw sockets could still be used while keeping the program platform independent.

    Cheers,
    cgkanchi
    Buy the Snakes of India book, support research and education (sorry the website has been discontinued)
    My blog: http://biology000.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •