0

我将以示例的形式解释我的查询。我有我的服务器(比如服务器 A),我需要连接到另一台服务器(比如服务器 B),以便我可以有时间并将其设置在我的服务器上。一旦设置,使用我的服务器的人应该得到新的时间(即来自服务器 B 的时间)。任何人都可以为我提供帮助。

我在这里发布的代码只是获取服务器时间并将其打印在客户端机器上,但没有将服务器时间设置为客户端机器。请提供帮助或链接。

客户端代码

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());
    }    
}

服务器端代码

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();
        }

}
4

0 回答 0