Example #1
0
 //---------------------------------------------------------------------------
 private void ItmCristian_Click(object sender, EventArgs e)
 {
     try
     {
         const string ntpServer = "time.windows.com";//default Windows time server
         IPAddress[]  addresses = Dns.GetHostEntry(ntpServer).AddressList;
         //The UDP port number assigned to NTP is 123
         IPEndPoint ipEndPoint = new IPEndPoint(addresses[0], 123); //NTP uses UDP
         // NTP message size - 16 bytes of the digest (RFC 1305)
         byte[] ntpData = new byte[48];
         //Setting the Leap Indicator, Version Number and Mode values
         ntpData[0] = 0x1B;                                     //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode) 0001
         DateTime OriginateSendTimestamp = System.DateTime.Now; //T1
         using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
         {
             socket.Connect(ipEndPoint);
             socket.ReceiveTimeout = 3000; //Stops code hang if NTP is blocked
             socket.Send(ntpData);
             socket.Receive(ntpData);      // nhan kq tra ve
             socket.Close();
         }
         DateTime OriginateReceiveTimestamp = System.DateTime.Now;                     //T4
         byte     Position         = 32;                                               //Received Time
         DateTime ReceiveTimestamp = DateTimeParser(ntpData, Position).ToLocalTime();  //T2
         Position = 40;
         DateTime TransmitTimestamp = DateTimeParser(ntpData, Position).ToLocalTime(); //T3
         long     Theta             = (long)Math.Round(((ReceiveTimestamp.Ticks - OriginateSendTimestamp.Ticks) +
                                                        (TransmitTimestamp.Ticks - OriginateReceiveTimestamp.Ticks)) / 2.0, 0);
         DateTime FinalDateTime = System.DateTime.Now.AddTicks(Theta);
         DateTimeUtils.SetDateTime(FinalDateTime.ToUniversalTime());
         FinalDateTime = System.DateTime.Now;
         TxtLogs.Clear();
         string FormatDateTime = "dd/MM/yyyy HH:mm:ss.ms";
         TxtLogs.AppendText("Originate Send Timestamp T1: " + OriginateSendTimestamp.ToString(FormatDateTime));
         TxtLogs.AppendText(Environment.NewLine);
         TxtLogs.AppendText("ReceiveTimestamp T2: " + ReceiveTimestamp.ToString(FormatDateTime));
         TxtLogs.AppendText(Environment.NewLine);
         TxtLogs.AppendText("Transmit Timestamp T3: " + TransmitTimestamp.ToString(FormatDateTime));
         TxtLogs.AppendText(Environment.NewLine);
         TxtLogs.AppendText("Originate Receive Time stamp T4: " + OriginateReceiveTimestamp.ToString(FormatDateTime));
         TxtLogs.AppendText(Environment.NewLine);
         TxtLogs.AppendText("Theta: " + Theta.ToString());
         TxtLogs.AppendText(Environment.NewLine);
         TxtLogs.AppendText("Final Time: " + FinalDateTime.ToString(FormatDateTime));
     }
     catch (Exception ex)
     {
         TxtLogs.AppendText(Environment.NewLine + ex.Message);
     }
 }
Example #2
0
        //---------------------------------------------------------------------------
        private DateTime DateTimeParser(byte[] ntpData, byte Position)
        {
            DateTime RetVal = new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc);//**UTC** time

            try
            {
                ulong intPart   = BitConverter.ToUInt32(ntpData, Position);     //Get the seconds part
                ulong fractPart = BitConverter.ToUInt32(ntpData, Position + 4); //Get the seconds fraction
                //Convert From big-endian to little-endian
                intPart   = SwapEndianness(intPart);
                fractPart = SwapEndianness(fractPart);
                ulong milliseconds = (intPart * 1000) + ((fractPart * 1000) / 0x100000000L);
                RetVal = RetVal.AddMilliseconds((long)milliseconds);
            }
            catch (Exception ex)
            {
                TxtLogs.AppendText(Environment.NewLine + ex.Message);
            }
            return(RetVal);
        }
 private void addLog(string text)
 {
     TxtLogs.AppendText(string.Format("{0}: {1}\n", DateTime.Now.ToString("hh:mm:ss"), text));
     TxtLogs.ScrollToEnd();
 }