Windows Server搭建局域网NTP时间服务器与客户端通实现

1.服务器环境:

win11更改注册表 win+R输入regedit

win11更改注册表 win+R输入regedit

2.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Config,找到Config目录,双击Config目录下的AnnounceFlags,设为5。

3.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer,将enabled设为1。

4.更改Windows服务
打开服务管理器 WIN+R运行 输入services.msc

步骤1:配置NTP服务器

1.打开命令提示符(以管理员身份运行)

2.配置NTP服务器

w32tm /config /manualpeerlist:{}, 0x8 /syncfromflags:MANUAL
注:{}内是你要同步的外部服务器地址,例如复旦的时间同步服务器地址为:ntp.fudan.edu.cn,则完整命令如下:

w32tm /config /manualpeerlist:ntp.fudan.edu.cn,0x8 /syncfromflags:MANUAL

国家授时中心服务器的IP地址(210.72.145.44)

ntp.fudan.edu.cn (复旦)推荐使用

time-b.nist.gov1 s1a.time.edu.cn 北京邮电大学

s1b.time.edu.cn 清华大学

s1c.time.edu.cn 北京大学

s1d.time.edu.cn 东南大学

s1e.time.edu.cn 清华大学

s2a.time.edu.cn 清华大学

s2b.time.edu.cn 清华大学

s2c.time.edu.cn 北京邮电大学

s2d.time.edu.cn 西南地区网络中心

s2e.time.edu.cn 西北地区网络中心

s2f.time.edu.cn 东北地区网络中心

s2g.time.edu.cn 华东南地区网络中心

s2h.time.edu.cn 四川大学网络管理中心

s2j.time.edu.cn 大连理工大学网络中心

s2k.time.edu.cn CERNET桂林主节点

s2m.time.edu.cn 北京大学


10.2.2.163:表示为局域网服务器IP

步骤2:启动Windows时间服务

1.重新启动Windows时间服务

net stop w32time

net start w32time

步骤3:强制同步时间

强制重新同步时间
w32tm /resync

C#客户端代码实现同步时间与
1. 同步NTP服务器时间的函数

using System;
using System.Diagnostics;

public class TimeSync
{
    public static bool SyncTime(string ntpServer)
    {
        try
        {
            // 配置NTP服务器
            if (!RunCommand($"w32tm /config /manualpeerlist:\"{ntpServer}\" /syncfromflags:manual /update"))
            {
                return false;
            }

            // 启动Windows时间服务
            if (!RunCommand("net start w32time"))
            {
                return false;
            }

            // 强制重新同步时间
            if (!RunCommand("w32tm /resync"))
            {
                return false;
            }

            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"同步时间时发生错误: {ex.Message}");
            return false;
        }
    }

    private static bool RunCommand(string command)
    {
        try
        {
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", $"/c {command}")
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            using (Process process = Process.Start(psi))
            {
                using (System.IO.StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"执行命令 '{command}' 时发生错误: {ex.Message}");
            return false;
        }
    }
}

2. 校验NTP服务器时间的函数

using System;
using System.Diagnostics;

public class TimeCheck
{
    public static bool CheckTime()
    {
        try
        {
            // 检查同步状态
            return RunCommand("w32tm /query /status");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"校验时间时发生错误: {ex.Message}");
            return false;
        }
    }

    private static bool RunCommand(string command)
    {
        try
        {
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", $"/c {command}")
            {
                RedirectStandardOutput = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            using (Process process = Process.Start(psi))
            {
                using (System.IO.StreamReader reader = process.StandardOutput)
                {
                    string result = reader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine($"执行命令 '{command}' 时发生错误: {ex.Message}");
            return false;
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值