public class PortScannerGUIApp extends JFrame
1: //PortScannerGUIApp.java
2: //Scans ports from numbers 1 through 256 and
3: //reports those which are active.
5: import java.awt.*;
6: import java.awt.event.*;
7: import java.net.*;
8: import java.io.*;
9: import javax.swing.*;
11: public class PortScannerGUIApp extends JFrame
12: implements ActionListener
13: {
14: private JTextField hostInput;
15: private JTextArea report;
16: private JScrollPane scrollPane;
17: private JButton button;
20: public static void main(String [] args)
21: {
22: PortScannerGUIApp app = new PortScannerGUIApp();
23: app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
24: app.makeGUI();
25: app.setSize(600, 380);
26: app.setVisible(true);
27: }
30: public void makeGUI()
31: {
32: getContentPane().setLayout(new FlowLayout());
34: button = new JButton("Enter host name below and click here to " +
35: "display server ports found on that host:");
36: getContentPane().add(button);
38: hostInput = new JTextField("cs.stmarys.ca", 50);
39: getContentPane().add(hostInput);
41: report = new JTextArea(50, 50);
42: scrollPane = new JScrollPane(report,
43: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
44: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
45: getContentPane().add(scrollPane);
46: button.addActionListener(this);
47: }
48:
49: public void actionPerformed(ActionEvent event)
50: {
51: report.setText("");
52: try
53: {
54: //Check to see if the host exits ...
55: String host = hostInput.getText();
56: InetAddress address = InetAddress.getByName(host);
57:
58: //If OK, then continue ...
59: for (int i=0; i<256; i++)
60: {
61: try
62: {
63: Socket socket = new Socket(host, i);
64: report.append("There is a server on port " +
65: i + "\n");
66: socket.close();
67: }
68: catch (IOException e)
69: {
70: //Disabled to avoid excessive output.
71: //No server on that port ...
72: //report.append("Exception: Problem on port " +
73: // i + ": " + e.toString() + ".\n");
75: }
76: }
77: }
78: catch (UnknownHostException e)
79: {
80: report.setText("Error: Unknown host.");
81: }
82: report.setCaretPosition(0);
83: }
84: }