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:
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());
}
}
public class NtpClient
{
public static DateTime GetNetworkTime()
{
return GetNetworkTime(" time-nw.nist.gov");
return GetNetworkTime(" 64.90.182.55 ");
}
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);
}
public static DateTime GetNetworkTime(IPEndPoint ep)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
s.Connect(ep);
byte[] ntpData = new byte[48];
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;
}
}
}