1import java.net.*;
2
3public class SimplePing
4{
5 public static void main( String[] args )
6 {
7 System.out.println( "\nUsage: java SimplePing host [port] \n" +
8 " e.g.: java SimplePing www.torsten-horn.de \n" +
9 " or: java SimplePing 1.2.3.4 80 \n" );
10 try {
11 if( args.length <= 0 )
12 throw new Exception( "Parameter missing!" );
13 InetAddress host = InetAddress.getByName( args[0] );
14 int port = ( args.length > 1 ) ? Integer.parseInt( args[1] ) : 80;
15 long tm = System.nanoTime();
16 Socket so = new Socket( host, port );
17 so.close();
18 tm = (System.nanoTime() - tm) / 1000000L;
19 System.out.println( "Connection ok (port " + port + ", time = " + tm + " ms). \n" +
20 "Host Address = " + host.getHostAddress() + "\n" +
21 "Host Name = " + host.getHostName() );
22 System.exit( 0 );
23 } catch( Exception ex ) {
24 System.out.println( "Error: " + ex.getMessage() );
25 System.exit( 1 );
26 }
27 }
28}
29