codeaddict

Untitled

Dec 16th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.  /*author: _where
  2.   * A port scanner that takes in three arguments, a host, a starting number for the port range, and the ending number in the range
  3.   * tried this with the arguments - 10.199.212.2, 8079, 8081, and was connected to 8080.
  4.   */
  5. import java.io.*;
  6. import java.net.*;
  7. public class PortScanner {
  8.     public static void main(String[] args) throws FileNotFoundException
  9.     {
  10.         Socket connect = null; long t = 0; PrintStream output = new PrintStream("output.log");
  11.         for (int i = Integer.parseInt(args[1]); i < Integer.parseInt(args[2]); i++)
  12.         {
  13.             try
  14.             {
  15.                 t = System.currentTimeMillis(); connect = new Socket(InetAddress.getByName(args[0]), i);
  16.             }
  17.             catch(IOException e)
  18.             {
  19.                 output.println("Connection could not be established on port - " + i + " after " + (System.currentTimeMillis() - t) + " ms");
  20.                 continue;
  21.             }
  22.             output.println("Connection established on port - " + i + " after " + (System.currentTimeMillis() - t) + " ms");
  23.             try {
  24.                 if (connect != null)
  25.                     connect.close();
  26.             } catch (IOException e) {
  27.                 // TODO Auto-generated catch block
  28.                 e.printStackTrace();
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment