Advertisement
DanFloyd

Probe

Sep 25th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1.  
  2. import java.net.*;
  3.  
  4. // Single port scanner
  5. public class Probe implements Runnable {
  6.    
  7.     private Socket socket;
  8.     private int port;
  9.     private int timeout; // timeout in milliseconds for the socket
  10.     private InetAddress host_address;
  11.    
  12.     // Constructor
  13.     public Probe (InetAddress host_address, int port, int timeout) throws PortException {
  14.         if(port <=0 || port > 65535)
  15.             throw new PortException("Invalid port number");
  16.         if(timeout < 0)
  17.             throw new PortException("Invalid timeout");
  18.         // setting properties
  19.         this.setPort(port);
  20.         this.setAddress(host_address);
  21.         this.setTimeout(timeout);
  22.     }
  23.    
  24.     // Getters and setters
  25.     public int getPort() { return port; }
  26.     public int getTimeout() { return timeout; }
  27.     public InetAddress getAddress() { return host_address; }
  28.     public void setPort(int v) { port = v; }
  29.     public void setTimeout(int v) { timeout = v; }
  30.     public void setAddress(InetAddress v) { host_address = v; }
  31.    
  32.     // Run method
  33.     @Override
  34.     public void run () {
  35.         try {
  36.             socket = new Socket();
  37.             socket.connect(new InetSocketAddress(this.getAddress(), this.getPort()), this.getTimeout());
  38.             System.out.println("Port [" + this.getPort() + "] \t\t OPEN");
  39.         } catch (Exception e){
  40.             // do nothing: the port is closed or some error happened
  41.         }
  42.     }
  43.    
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement