01: //***** Socket basics: first, "blocking" sockets
02: 
03: import java.net.Socket;
04: import java.net.UnknownHostException; 
05: import java.io.IOException;
06: 
07: public class PortProbe {
08:     public static void main(String[ ] args) {
09:         if (args.length < 1) {
10:            System.err.println("Usage: PortProbe <IP address>");
11:            return;
12:         }
13:         String host = args[0];
14:         // port numbers for common services
15:         int[ ] ports = {
16:              7,     // ping
17:             13,     // daytime
18:             21,     // ftp
19:             23,     // telnet
20:             71,     // finger
21:             80,     // http
22:            119,     // nntp (news)
23:            161      // snmp
24:         };
25:         // Probe each port.
26:         for (int i = 0; i < ports.length; i++) {
27:             try {
28:                 Socket sock = new Socket(host, ports[i]);
29:                 System.out.println(host + " listening on port " + ports[i]);
30:                 sock.close();
31:             }
32:             catch(UnknownHostException e) {
33:                 System.err.println(e);
34:                 return;
35:             }
36:             catch(IOException e) {
37:                 System.out.println(host + " not listening on port " 
38:                     + ports[i]);
39:             }
40:         }
41:     }
42: }