Monday, October 12, 2015

Port Scanner (GUI)

I've made a port scanner but it seems to freeze until the whole scanning is finished. Maybe I need the multi-thread programming for solving this problem...but multi-thread seems hard for me.

code:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class PortScanner extends JFrame {
 JTextArea textarea;


 private JPanel contentPane;
 private JTextField textField;
 private JTextField textField_1;
 private JLabel lblPortScanner;
 private final Action action = new SwingAction();
 private JLabel lblNewLabel_2;

 /**
  * Launch the application.
  */
 public static void main(String[] args) {
 
  EventQueue.invokeLater(new Runnable() {
   public void run() {
    try {
     PortScanner frame = new PortScanner();
     frame.setVisible(true);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 
 }

 /**
  * Create the frame.
  */
 public PortScanner() {
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  setBounds(100, 100, 450, 500);
  contentPane = new JPanel();
  contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  setContentPane(contentPane);
  contentPane.setLayout(null);
 
  textField = new JTextField();
  textField.setBounds(133, 71, 167, 19);
  contentPane.add(textField);
  textField.setColumns(10);
 
  JLabel lblNewLabel = new JLabel("From");
  lblNewLabel.setBounds(171, 48, 80, 13);
  contentPane.add(lblNewLabel);
 
  textField_1 = new JTextField();
  textField_1.setBounds(133, 123, 167, 19);
  contentPane.add(textField_1);
  textField_1.setColumns(10);
 
  JLabel lblNewLabel_1 = new JLabel("To");
  lblNewLabel_1.setBounds(171, 100, 68, 13);
  contentPane.add(lblNewLabel_1);
 
  lblPortScanner = new JLabel("Port Scanner");
  lblPortScanner.setBounds(170, 25, 87, 13);
  contentPane.add(lblPortScanner);
 
  textarea = new JTextArea();
  textarea.setBounds(43, 195, 355, 240);
  contentPane.add(textarea);
 
  JButton btnGo = new JButton("Go");
  btnGo.setAction(action);
  btnGo.setBounds(177, 163, 80, 21);
  contentPane.add(btnGo);
 
  lblNewLabel_2 = new JLabel("");
  lblNewLabel_2.setBounds(159, 141, 115, 13);
  contentPane.add(lblNewLabel_2);
 }
 private class SwingAction extends AbstractAction {
 
  public SwingAction() {
   putValue(NAME, "Go");
   putValue(SHORT_DESCRIPTION, "Some short description");
  }
  public void actionPerformed(ActionEvent e) {
   String textFieldValue = textField.getText();
   String textFiledValue1 = textField_1.getText();
   String host = "localhost";
   lblNewLabel_2.setText("Scanning...");

     try {
       InetAddress theAddress = InetAddress.getByName(host);
       for (int i = Integer.parseInt(textFieldValue); i < Integer.parseInt(textFiledValue1); i++) {
       try {
         Socket theSocket = new Socket(theAddress, i);
            textarea.append("There is a server on port " + i + " of " + host + "\n");
                 
            }
            catch (IOException ex) {
               }
       }
     }
     catch(UnknownHostException ex) {
      System.err.println(ex);
     }
  }
 }
}