FileDocCategorySizeDatePackage
TCPTimeServer.javaAPI DocExample1661Thu Feb 16 11:57:58 GMT 2006None

TCPTimeServer

public class TCPTimeServer extends MIDlet

Fields Summary
private ServerSocketConnection
server
private long
differenceBetweenEpochs
Constructors Summary
Methods Summary
protected voiddestroyApp(boolean unconditional)

    try {
        server.close();
    }
    catch (IOException ex) {
        // We tried;
    }
  
protected voidpauseApp()

protected voidstartApp()

  
     
    try {
      server = (ServerSocketConnection) Connector.open("socket://:37");
      Runnable r = new Runnable() {
        public void run() {
          while (true) {
            try {
              StreamConnection conn = server.acceptAndOpen();
              Date now = new Date();
              long msSince1970 = now.getTime();
              long secondsSince1900 = msSince1970/1000L + differenceBetweenEpochs;
              DataOutputStream out = conn.openDataOutputStream();
              // write the low order four bytes
              out.write( (int) ((secondsSince1900 >>> 24) & 0xFFL));
              out.write( (int) ((secondsSince1900 >>> 16) & 0xFFL));
              out.write( (int) ((secondsSince1900 >>> 8) & 0xFFL));
              out.write( (int) (secondsSince1900 & 0xFFL));
              out.close();
            }
            catch (IOException ex) {
            }
          } 
        }
      };
      Thread t = new Thread(r);
      t.start();
    }
    catch (IOException ex) {
      // not much we can do about this here
    }