0

I will explain my query in the form of an example. I have my server(say server A),I need to connect to another server(say server B) so that I can have its time and set it on mine.Once set,the people using my server are suppose to get the new time(i.e the time from server B).Could anyone provide me with help.

The code I am posting here just gets the server time and prints it on the client machine but does not set the server time to the client machine.please provide help or links.

Client side code

class clienttime
{
    public static void main(String args[]) throws Exception
    {
        //InetAddress locIP = InetAddress.getByName("192.168.1.55");
        InetAddress locIP = InetAddress.getByName("127.0.0.1");
        Socket soc= new Socket(locIP,123);
        BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
        System.out.println(in.readLine());
    }    
}

Server side code

class servertime
{
    public static void main(String args[]) throws Exception
    {
        InetAddress locIP = InetAddress.getByName("127.0.0.1");
        ServerSocket s= new ServerSocket(7681, 0, locIP);
        //ServerSocket s=new ServerSocket(5217);
        while(true)
        {
            System.out.println("Waiting For Connection ...");
            Socket soc=s.accept();
            DataOutputStream out=new DataOutputStream(soc.getOutputStream());
            String x = new Date().toString();
            out.writeBytes("Server Date " + (new Date()).toString() + "\n");
            out.close();
            soc.close();
        }

}
Santino 'Sonny' Corleone
  • 1,735
  • 5
  • 25
  • 52
  • 1
    Given that you are aware how to get the time from the server I'd rephrase the question as "How do I set the system time given a string representing a `Date` object?" – nanofarad Nov 12 '13 at 11:37
  • @hexafraction could you provide me with any answer? – Santino 'Sonny' Corleone Nov 12 '13 at 11:55
  • 1
    http://stackoverflow.com/questions/6203857/how-can-i-set-the-system-time-in-java explains the difficulties in setting the system time in a system-independent environment like the JVM, and some alternatives. Converting from a string date to a date object is covered in innumerable tutorials from here to breakfast. – arcy Nov 12 '13 at 12:37

0 Answers0