Click here to Skip to main content
16,007,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone,

I really need your help.

I have writen a C# program, and I would like to connect it to the internet and get current time and date from a site like time-a.nist.gov, because I don't need local system time and date.
When I use this program, I don't know what should writen in main method of my console and when I use this code.

The following error appeared:
Error     The type or namespace name 'SPOT' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)

Can you please help me to solve it?

Please help me and give me your suggestion.

My program is here:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
 
namespace MFToolkit.Net.Ntp
{
 
      class Program
      {
 
            static void Main(string[] args)
            {
 

                  Microsoft.SPOT.ExtendedTimeZone.SetTimeZone(TimeZoneId.Berlin);
                  Microsoft.SPOT.Hardware.Utility.SetLocalTime(GetNetworkTime());
            }
 
      }
 
      /// <summary>
      /// Static class to receive the time from a NTP server.
      /// </summary>
      public class NtpClient
      {
            /// <summary>
            /// Gets the current DateTime from time-a.nist.gov.
            /// </summary>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime()
            {
                  // return GetNetworkTime("time-a.nist.gov");
                  return GetNetworkTime(" time-nw.nist.gov");
                  //nist1-ny.ustiming.org 
                  return GetNetworkTime(" 64.90.182.55 ");
            }
 
            /// <summary>
            /// Gets the current DateTime from <paramref name="ntpServer" />.
            /// </summary>
            /// <param name="ntpServer">The hostname of the NTP server.</param>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime(string ntpServer)
            {
                  IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;
 
                  if (address == null || address.Length == 0)
                        throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");
 
                  IPEndPoint ep = new IPEndPoint(address[0], 123);
 
                  return GetNetworkTime(ep);
            }
 
            /// <summary>
            /// Gets the current DateTime form <paramref name="ep" /> IPEndPoint.
            /// </summary>
            /// <param name="ep">The IPEndPoint to connect to.</param>
            /// <returns>A DateTime containing the current time.</returns>
            public static DateTime GetNetworkTime(IPEndPoint ep)
            {
                  Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
                  s.Connect(ep);
 
                  byte[] ntpData = new byte[48]; // RFC 2030
                  ntpData[0] = 0x1B;
                  for (int i = 1; i < 48; i++)
                        ntpData[i] = 0;
 
                  s.Send(ntpData);
                  s.Receive(ntpData);
 
                  byte offsetTransmitTime = 40;
                  ulong intpart = 0;
                  ulong fractpart = 0;
 
                  for (int i = 0; i <= 3; i++)
                        intpart = 256 * intpart + ntpData[offsetTransmitTime + i];
 
                  for (int i = 4; i <= 7; i++)
                        fractpart = 256 * fractpart + ntpData[offsetTransmitTime + i];
 
                  ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
                  s.Close();
 
                  TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
 
                  DateTime dateTime = new DateTime(1900, 1, 1);
                  dateTime += timeSpan;
 
                  TimeSpan offsetAmount = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
                  DateTime networkDateTime = (dateTime + offsetAmount);
 
                  return networkDateTime;
 
                  //System.Threading.Thread.Sleep(1000);
 
            }
      }
}
Posted
Updated 6-Nov-11 1:46am
v2

 
Share this answer
 
Comments
thatraja 6-Nov-11 8:51am    
Nice alternates, 5!
 
Share this answer
 
v3
Comments
sokh 6-Nov-11 8:24am    
yes thanks,But in references,.Net microsoft part there isn't any Spot part!
Do you know any solution for solving this?
thatraja 6-Nov-11 8:28am    
Is that(SPOT) a custom/3rd party dll?
If here before this was worked then you need to search that dll in your system.
Mehdi Gholam 6-Nov-11 8:34am    
I believe spot was a Microsoft watch.
thatraja 6-Nov-11 8:37am    
You right dude. Thanks.
thatraja 6-Nov-11 8:40am    
Check the updated answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900