C#_获取Internet时间(非本地时间)

远程连接时间服务器,获得时间后,可以选择输出,也可以使用WinAPI,设置为系统时间,代码如下: 

using System;
using System.Data;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.Net.Sockets;

namespace getInterTime
{
    struct SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;

        /// <summary>   
        /// 从System.DateTime转换。   
     /// </summary>   
        /// <param name="time">System.DateTime类型的时间。</param>   
        public void FromDateTime(DateTime time)
        {
            wYear = (ushort)time.Year;
            wMonth = (ushort)time.Month;
            wDayOfWeek = (ushort)time.DayOfWeek;
            wDay = (ushort)time.Day;
            wHour = (ushort)time.Hour;
            wMinute = (ushort)time.Minute;
            wSecond = (ushort)time.Second;
            wMilliseconds = (ushort)time.Millisecond;
        }
        /// <summary>   
        /// 转换为System.DateTime类型。   
      /// </summary>   
        /// <returns></returns>   
        public DateTime ToDateTime()
        {
            return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
        }
        /// <summary>   
        /// 静态方法。转换为System.DateTime类型。   
        /// </summary>   
        /// <param name="time">SYSTEMTIME类型的时间。</param>   
        /// <returns></returns>   
        public static DateTime ToDateTime(SystemTime time)
        {
            return time.ToDateTime();
        }
    }
    class Win32API
    {
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime Time);
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime Time);
    }  
    class Program
    {
        static void Main(string[] args)
        {
            getInternetTime();
            Console.ReadLine();
        }

        public static void getInternetTime()
        {
            // 记录开始的时间   
            DateTime startDT = DateTime.Now;

            //建立IPAddress对象与端口,创建IPEndPoint节点:   
            int port = 13;
            string[] whost = { "time-nw.nist.gov", "5time.nist.gov", "time-a.nist.gov", "time-b.nist.gov", "tick.mit.edu", "time.windows.com", "clock.sgi.com" };

            IPHostEntry iphostinfo;
            IPAddress ip;
            IPEndPoint ipe;
            Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建Socket
            c.ReceiveTimeout = 10 * 1000;//设置超时时间   
            string sEX = "";// 接受错误信息   
            // 遍历时间服务器列表   
            foreach (string strHost in whost)
            {
                try
                {
                    iphostinfo = Dns.GetHostEntry(strHost);
                    ip = iphostinfo.AddressList[0];
                    ipe = new IPEndPoint(ip, port);
                    c.Connect(ipe);//连接到服务器   
                    if (c.Connected) break;// 如果连接到服务器就跳出   
                }
                catch (Exception ex)
                {
                   
                }
            }
            //SOCKET同步接受数据   
            byte[] RecvBuffer = new byte[1024];
            int nBytes, nTotalBytes = 0;
            StringBuilder sb = new StringBuilder();
            System.Text.Encoding myE = Encoding.UTF8;
            try
            {
                while ((nBytes = c.Receive(RecvBuffer, 0, 1024, SocketFlags.None)) > 0)
                {
                    nTotalBytes += nBytes;
                    sb.Append(myE.GetString(RecvBuffer, 0, nBytes));
                }
            }
            catch (System.Exception ex)
            {
            	
            }
           
            //关闭连接   
            c.Close();
            string[] o = sb.ToString().Split(' '); // 打断字符串   
            TimeSpan k = new TimeSpan();
            k = (TimeSpan)(DateTime.Now - startDT);// 得到开始到现在所消耗的时间   
            DateTime SetDT = Convert.ToDateTime(o[1] + " " + o[2]).Subtract(-k);// 减去中途消耗的时间   

            //处置北京时间 +8时   
            SetDT = SetDT.AddHours(8);

            //转换System.DateTime到SystemTime   
            SystemTime st = new SystemTime();
            st.FromDateTime(SetDT);
            Console.WriteLine(st.ToDateTime().ToString()); //这里可以输出获得的互联网时间
            //Win32API.SetLocalTime(ref st);//如果选择不输出,也可以使用API设置为系统的时间,API函数申明见上面的Win32API类   
        }
    }
}

 


下载:getInterTime.7z

转载于:https://www.cnblogs.com/lijialong/archive/2010/06/06/GetInternetTime.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值