|
The following code changes seem to get the socket calls to not error out, but I am still validating that the resulting data is correct:
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPHostEntry hostEntry = Dns.GetHostEntry(TimeServer);
IPEndPoint listenEP = new IPEndPoint(hostEntry.AddressList[0], 123);
IPEndPoint sendEP = new IPEndPoint(hostEntry.AddressList[0], 123);
EndPoint epSendEP = (EndPoint)sendEP;
int messageLength = 0;
try
{
sendSocket.Connect(listenEP);
Looking back on this, it may be possible to simplify using the UdpClient class.
|
|
|
|
|
The problem in "W32Time" service, or other program, that already use 123 port.
You can check it like this:
bool isInUsed = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveUdpListeners().Any(p => p.Port == 123);
modified 16-Mar-16 14:22pm.
|
|
|
|
|
I found today that the class works just as well today as when written. A couple of very minor changes to the NTPClient class for depricated methods and a few minor changes to suit my own project and I have a very handy tool. Great Job!
|
|
|
|
|
It can be old but really very helpful. Thanks.
|
|
|
|
|
Hi ,
thanks for such a wonderfull explaination , can you tell me IP address for IST i am trying to find but i am not able to . Thanks for your time .
Regards,
Sagar
|
|
|
|
|
how can I execute this program to sync my system whit internet time servers after execute?
should I change this program or use dll Import?
plz help me .I cant write & understand En good.
|
|
|
|
|
|
Hi
How can you implement a timeout into this?
If the IP is not a NTP server then the app hangs at NTPData = TimeSocket.Receive(EPhost) as there's no data to receive
cheers
|
|
|
|
|
This have been replied in previous discutions, by using the
TimeSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
But this throw an exception on compact framework with the SocketErrorCode = 10042 (not supported option).
According to msdn doc, there is no way to set this option on windows ce/mobile platforms...
If somebody have a solution ... Please give it to me !
Romain TAILLANDIER
www.romaintaillandier.blogspot.com
romaintaillandier.free.fr
www.maintag.fr
|
|
|
|
|
|
hmmm ...
I get the solution, while you reply me !
Let me post it :
Instead of
NTPData = TimeSocket.Receive(ref EPhost);
(wich can block indefinitely), I wrote this :
IAsyncResult R = TimeSocket.Client.BeginReceive(NTPData, 0, NTPData.Length, SocketFlags.None, null, null);
if(R.AsyncWaitHandle.WaitOne(5000,false))
{
try
{
TimeSocket.Client.EndReceive(R);
}
finally
{
TimeSocket.Close();
}
}
else
{
TimeSocket.Close();
return;
}
I will take some time to compare with your solution
Romain TAILLANDIER
www.romaintaillandier.blogspot.com
romaintaillandier.free.fr
www.maintag.fr
|
|
|
|
|
You need to dispose the UdpSocket, or you'll leak.
//Connect the time server
using (UdpClient TimeSocket = new UdpClient())
{
TimeSocket.Connect(EPhost);
Otherwise... it's definitely cool!-bruce
|
|
|
|
|
I noticed there is no license specified for this code. Can you kindly provide license terms? Thanks.
|
|
|
|
|
Just use the code and if you really like it, mention my name. That's it.
Delta Forth .NET (www.dataman.ro)
World's first Forth compiler for the .NET platform
|
|
|
|
|
For this useful article and source code,
thanks in advance...
|
|
|
|
|
if no ip by this address the program is stop to long. can you help?
|
|
|
|
|
by enter ip directly i mean, i m not use dns.
|
|
|
|
|
system: WINCE5.0
stop (hangs) here: NTPData = TimeSocket.Receive(...
|
|
|
|
|
Hi Valer,
what are the conditions to use your code in commercial environments ?
Harm Jonker
|
|
|
|
|
Send me a message from my blog at www.dataman.ro.
Regards,
Valer
Delta Forth .NET (www.dataman.ro)
World's first Forth compiler for the .NET platform
|
|
|
|
|
Has anyone experienced any problems getting the correct time if the device/computer if the querying computer is set wrongly in another daylight savings period?
i.e. if my computer clock is set to Jan 7th 2008 08:00, and i run the code on April 7th 08:00 - my clock will be set to April 7th 09:00 - one hour off. This does not happen if i run this in a date in the same daylight savings period.
Alex
|
|
|
|
|
Hi Alex
Yes I have just had the same problem. It turns out when the code is correcting the date/time for the different time zone it is using the current date/time of the querying computer. If the querying computer is in a different time zone then the offset can be out by 1 hour.
The problem can be solved by replacing the calls to GetUtcOffset() as below:
Replace:
// Take care of the time zone
TimeSpan offspan = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now);
return time + offspan;
With:
// Take care of the time zone
TimeZone currentTimeZone = TimeZone.CurrentTimeZone;
return currentTimeZone.ToLocalTime( time );
This now converts the UTC time to the local time without using the querying computers clock.
Regards
Geoff
modified on Friday, June 6, 2008 12:18 AM
|
|
|
|
|
The program block, when I compile in debug, it's at line :
NTPData = TimeSocket.Receive(ref EPhost);
because I recieve nothing from the serveur...
Someone have an idea ?
|
|
|
|
|
I think someone has already mentioned this, but to use this in Compact Framework, you'll need to change kernel32 to coredll.dll. Also, I found that Active Sync would block NTP. It took me a while to figure that problem out, so I hope it helps somebody else.
|
|
|
|
|
The way that I handled this was to not have the device Active Sync'ed when I called NTP. This wasn't an issue for me because for this project, the devices were never Active Sync'ed after development. I supppose, and when I say suppose, I mean wild guess. If you really had to get around this, you could write an app that was both server and client with a normal port on the client side, and a variable port on the server side so you could find a port that was open. That would be a pretty big project. If the time doesn't need to be so exact, you could probably just do it over a web service.
I should also point out that in Compact Framework, the UDPClient has no timeout. I tried running this in a seperate thread, but we ended up just running NTP in a seperate exe so the main app didn't run out of threads.
Good luck.
|
|
|
|