Friday, March 14, 2014

Cristian's Algorithm for clock synchronization

The following programs implement the basic form of Cristian's algorithm for physical clock synchronization.
It is the basic form of clock synchronization.
Run the server first then run the client.



ClockServer.java

import java.io.*;
import java.net.*;
import java.util.*;

/**
 *
 * @author student
 */
public class ClockServer {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) throws IOException {
        String port;
        BufferedReader stdIn =
                new BufferedReader(
                    new InputStreamReader(System.in));
        System.out.println("Enter the port no");
       
        port=stdIn.readLine();
               
        int portNumber = Integer.parseInt(port);
        
        try (
            ServerSocket serverSocket =
                new ServerSocket(portNumber);
            Socket clientSocket = serverSocket.accept();     
            PrintWriter out =
                new PrintWriter(clientSocket.getOutputStream(), true);                   
            BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));
            
        ) {
            String inputLine;
            System.out.println("Server Started");
            while (true) {
                inputLine = in.readLine();
                if(inputLine.equalsIgnoreCase("Exit"))
                {
                     System.out.println("Exiting");
                     out.println("Server Exiting");
                     break;
                }
                out.println(System.currentTimeMillis()+5000);
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "
                + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }

}


ClockClient.java

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;
/**
 *
 * @author student
 */
public class ClockClient {

    /**
     * @param args the command line arguments
     */
public static void main(String[] args) throws IOException {
        
        String port,hostName;
        BufferedReader stdIn =
                new BufferedReader(
                    new InputStreamReader(System.in));
        System.out.println("Enter the port no");
        port=stdIn.readLine();
        int portNumber = Integer.parseInt(port);
        System.out.println("Enter the host name");
        hostName=stdIn.readLine();
        try (
            Socket echoSocket = new Socket(hostName, portNumber);
            PrintWriter out =
                new PrintWriter(echoSocket.getOutputStream(), true);
            BufferedReader in =
                new BufferedReader(
                    new InputStreamReader(echoSocket.getInputStream()));
            ) {
            String userInput;
            System.out.println("Client Started");
            System.out.println("Enter Exit to stop");
            
                long T0;
                long serverTime;
                long T1;
                long finalTime;                
                out.println(T0=System.currentTimeMillis());
                serverTime = Long.parseLong(in.readLine());
                T1 =System.currentTimeMillis();
                finalTime =  serverTime + (T1-T0)/2;
                DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
                System.out.println("Client Time: " + formatter.format(new Date(T1)));
                System.out.println("Server Time: " + formatter.format(new Date(serverTime)));
                System.out.println("Client Time after reset: " + formatter.format(new Date(finalTime)));
                out.println("EXit");
                
               
           
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                hostName);
            System.exit(1);
        } 
    }

}


Output:


Server
Enter the port no
2222
Server Started

Exiting


Client
Enter the port no
2222
Enter the host name
localhost
Client Started
Enter Exit to stop
Client Time: 13:53:23:515
Server Time: 13:53:28:515

Client Time after reset: 13:53:28:515