public class PortScanner
1: // PortScanner.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.*;
10: public class PortScanner
11: extends Frame
12: implements ActionListener
13: {
14: private TextField hostInput;
15: private TextArea report;
16: private Button button, exit;
19: public static void main(String [] args)
20: {
21: PortScanner lfp = new PortScanner();
22: lfp.makeGUI();
23: lfp.setSize(525, 280);
24: lfp.setVisible(true);
25: }
28: public void makeGUI()
29: {
30: setLayout(new FlowLayout());
32: button = new Button("Enter host name below and click here to " +
33: "display server ports found on that host:");
34: add(button);
36: hostInput = new TextField("cs.stmarys.ca", 40);
37: add(hostInput);
39: report = new TextArea("", 0, 0,
40: TextArea.SCROLLBARS_VERTICAL_ONLY);
41: add(report);
42: button.addActionListener(this);
44: exit = new Button("Exit");
45: add(exit);
46: exit.addActionListener(this);
48: }
50:
51: public void actionPerformed(ActionEvent event)
52: {
53: if (event.getSource() == exit)
54: System.exit(0);
56: report.setText("");
57: String host = hostInput.getText();
58: // try
59: // {
60: //InetAddress theAddress = InetAddress.getByName(host);
61: for (int i=0; i<256; i++)
62: {
63: try
64: {
65: Socket socket = new Socket(host, i);
66: report.append("There is a server on port " +
67: i + "\n");
68: socket.close();
69: }
70: catch (IOException e)
71: {
72: // No server on that port ...
73: // report.append("Exception: Problem on port " +
74: // i + ": " + e.toString());
76: }
77: }
78: // }
79: // catch (UnknownHostException e)
80: // {
81: // report.setText("Unknown host");
82: // }
83: }
84: }