• .NET实现获取NTP服务器时间并同步(附带Windows系统启用NTP服务功能)


    对某个远程服务器启用和设置NTP服务(Windows系统)

    打开注册表

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer

    将 Enabled 的值设置为 1,这将启用NTP服务器功能。

    防火墙开放UDP 123端口

    打开“服务”应用(可以在开始菜单搜索“服务”),找到“Windows Time”服务。右键点击“Windows Time”服务,选择“重启”。

    执行以下命令来配置服务器模式并重启时间服务:

    w32tm /config /reliable:YES /update
    net stop w32time
    net start w32time

     

    客户端上面,输入以下命令,替换你自己的NTP服务端IP即可,如果显示类似以下的时间输出,说明是正常的。

    w32tm /stripchart /computer:ip地址 /samples:5 /dataonly

     

     编写程序,进行时间同步

    引用包Wesky.Net.OpenTools ,版本选择1.0.6或以上版本。

     

    该包的自述文件内容供参考:

    https://www.nuget.org/packages/Wesky.Net.OpenTools/1.0.6#readme-body-tab

     在程序里面使用,以下测试内容供参考。其中,ntpServer可以是ip地址也可以是ntp服务器的域名地址或者互联网ntp服务器地址等。获取时间默认端口号没指定的话是123,如果要指定,只需要在参数里面新增端口号参数即可。

     

    复制代码
    static void Main(string[] args)
    {
        string ntpServer = "ip";
        Console.WriteLine($"当前时间:\r\n{ DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ms")}");
        DateTime time = NtpClient.GetNtpServerTime(ntpServer);
        Console.WriteLine($"获取到的时间为:\r\n {time.ToString("yyyy/MM/dd HH:mm:ss ms")}");
        NtpClient.SetSystemTime(time);
        Console.WriteLine($"更改后的系统时间:\r\n{ DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss ms")}");
        Console.ReadLine();
    }
    复制代码

    获取ntp服务器时间核心代码解析如下:

    复制代码
     1 /// 
     2 /// 获取NTP服务器的时间。
     3 /// Retrieves the time from an NTP server.
     4 /// 
     5 /// NTP服务器地址 | NTP server address'
     6 /// NTP服务的端口 | NTP service port
     7 /// 服务器时间 | Server time
     8 public static DateTime GetNtpServerTime(string ntpServer,int ntpPort=123)
     9 {
    10     // 初始化NTP数据缓冲区
    11     // Initialize NTP data buffer
    12     byte[] ntpData = new byte[NtpDataLength];
    13     ntpData[0] = 0x1B; // NTP version number (3) and mode (3), client request
    14 
    15     var addresses = Dns.GetHostAddresses(ntpServer);
    16     IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], ntpPort);
    17 
    18     using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    19     {
    20         socket.Connect(ipEndPoint);
    21         socket.Send(ntpData);
    22         socket.Receive(ntpData);
    23     }
    24 
    25     // 从字节40和44提取时间戳
    26     // Extract timestamp from bytes 40 and 44
    27     ulong intPart = BitConverter.ToUInt32(ntpData, 40);
    28     ulong fractPart = BitConverter.ToUInt32(ntpData, 44);
    29 
    30     // 转换字节序为小端格式
    31     // Convert byte order to little endian
    32     intPart = SwapEndianness(intPart);
    33     fractPart = SwapEndianness(fractPart);
    34 
    35     var milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
    36 
    37     // NTP时间是从1900年开始计算的,这里将其转换为UTC时间
    38     // NTP time starts from 1900, this converts it to UTC DateTime
    39     DateTime networkDateTime = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)milliseconds);
    40 
    41     return networkDateTime.ToLocalTime();
    42 }
    复制代码

     

  • 相关阅读:
    yamot:一款功能强大的基于Web的服务器安全监控工具
    spring boot 面试题
    matlab函数 状态空间系统ss、能控性矩阵ctrb、矩阵的秩rank、能控标准型canon、零极点配置place、系统极点pole等函数(线性定常系统)
    java计算机毕业设计甜趣网上蛋糕店订购系统源码+系统+数据库+lw文档+mybatis+运行部署
    关于Java8 stream的相关使用及解析
    国际短信平台哪家好?
    Linux ubuntu 20.04.5 Server安装远程桌面
    kernel panic log
    测试工程师面试题,你都遇到过哪些呢?
    【性能测试】MySQL慢查询原因/排查思路+SQL优化与性能定位思路...
  • 原文地址:https://www.cnblogs.com/weskynet/p/18194418