FileDocCategorySizeDatePackage
UDPTimeClient.javaAPI DocExample1798Sat Sep 09 20:52:06 BST 2000None

UDPTimeClient

public class UDPTimeClient extends Object

Fields Summary
public static final int
DEFAULT_PORT
public static final String
DEFAULT_HOST
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

  
       
    
    InetAddress host;
    int port = DEFAULT_PORT;

    try {
      if (args.length > 0) {
        host = InetAddress.getByName(args[0]);
      }
      else {
        host = InetAddress.getByName(DEFAULT_HOST);
      } 
    }
    catch (Exception e) {
      System.out.println("Usage: java UDPTimeClient host port");
      return;
    }

    if (args.length > 1) {
      try {
        port = Integer.parseInt(args[1]);
        if (port <= 0 || port > 65535) port = DEFAULT_PORT;;
      }
      catch (Exception e){
      }
    }

    try {
      UDPPoke poker = new UDPPoke(host, port);
      byte[] response = poker.poke();
      if (response == null) {
      	System.out.println("No response within allotted time");
      	return;
      }
      else if (response.length != 4) {
      	System.out.println("Unrecognized response format");
      	return;         
      }
   
      
      // The time protocol sets the epoch at 1900,
      // the java Date class at 1970. This number 
      // converts between them.
    
      long differenceBetweenEpochs = 2208988800L;

      long secondsSince1900 = 0;
      for (int i = 0; i < 4; i++) {
        secondsSince1900 = (secondsSince1900 << 8) | (response[i] & 0x000000FF);
      }

      long secondsSince1970 
       = secondsSince1900 - differenceBetweenEpochs;       
      long msSince1970 = secondsSince1970 * 1000;
      Date time = new Date(msSince1970);
      
      System.out.println(time);
    }
    catch (Exception e) {
      System.err.println(e);	
      e.printStackTrace();
    }