如何在C#中从服务器IP获取或同步日期和时间(后端代码)

时间:2018-12-24 12:53:24

标签: asp.net-mvc angular asp.net-web-api asp.net-web-api2

在C#中,我不会从服务器IP中获取或同步日期和时间(后端代码)  目前,我正在从NTP服务器获取时间和日期,但我希望服务器时间和日期来自公共IP。

这是我的获取NTP服务器时间的代码,但是如何转换此代码以获取服务器IP的时间和日期。

    private static void Main(string[] args)
   {

      foreach (string server in ListOfNTPServer())
      {
            DateTime dateTime = GetNetworkTime(server);
                Console.WriteLine(dateTime + "----" + server);
            }
            Console.ReadLine();

      }

        public static string[] ListOfNTPServer()
        {
            string[] serverList = {
                "time-a-g.nist.gov",
                "time-b-g.nist.gov",
                "time-c-g.nist.gov",
                "time-d-g.nist.gov",
                "time-d-g.nist.gov",
                "time-a-wwv.nist.gov",
                "time-b-wwv.nist.gov",
                "time-c-wwv.nist.gov",
                "time-d-wwv.nist.gov",
                "time-d-wwv.nist.gov",
                "time-a-b.nist.gov",
                "time-b-b.nist.gov",
                "time-c-b.nist.gov",
                "time-d-b.nist.gov",
                "time-d-b.nist.gov",
                "time.nist.gov",
                "utcnist.colorado.edu",
                "utcnist2.colorado.edu",
                "ntp-b.nist.gov",
                "ntp-wwv.nist.gov",
                "ntp-c.colorado.edu",
                "ntp-d.nist.gov",
                "ut1-time.colorado.edu"
            };

            return serverList;
        }

        public static DateTime GetNetworkTime(string ntpServer)
        {
            try
            {

                // const string ntpServer = "time-a-wwv.nist.gov";
                byte[] ntpData = new byte[48];
                ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)

                IPAddress[] addresses = Dns.GetHostEntry(ntpServer).AddressList;
                IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123);
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                socket.Connect(ipEndPoint);
                socket.Send(ntpData);
                socket.Receive(ntpData);
                socket.Close();

                ulong intPart = (ulong)ntpData[40] << 24 | (ulong)ntpData[41] << 16 | (ulong)ntpData[42] << 8 | ntpData[43];
                ulong fractPart = (ulong)ntpData[44] << 24 | (ulong)ntpData[45] << 16 | (ulong)ntpData[46] << 8 | ntpData[47];

                ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
                DateTime networkDateTime = (new DateTime(1900, 1, 1)).AddMilliseconds((long)milliseconds);

                return networkDateTime;


            }
            catch (Exception)
            {

                return DateTime.Now;
            }
        }

谢谢。

1 个答案:

答案 0 :(得分:0)

还在服务器列表中维护服务器的时区

class server
    {
        public string hostname { get; set; }
        public double hourdiff { get; set; }
    }

    List<server> serverList = new List<server>();
    serverList.Add(new server { hostname = "time-a-g.nist.gov", hourdiff = 5.5 });// for india time


public static DateTime GetNetworkTime(string ntpServer)
{
   .......
   server serv = serverList.Where(x => x.hostname == ntpServer).FirstOrDefault();
            DateTime servertime = DateTime.UtcNow.AddHours(serv.hourdiff);
    ........
}