|
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.
|
|
|
|
|
Use this for desktop and compact framework compability. ActiveSync is blocking UPD transfers. Using a mobile device, you can only rely on internal network cards. Using the internal wifi card als network interface, NTP works like a charm. Instead of NTP there is the daytime protocol available. Some windows servers run this service by default (Port 13) (RFC 867).
telnet 192.168.0.1 13
Use the code below for desktop and compact framework compability. ActiveSync is blocking UPD transfers. Using a mobile device, you can only rely on internal network cards. Using the internal wifi card als network interface, NTP works like a charm. Instead of NTP there is the daytime protocol available. Some windows servers run this service by default (RFC 867). You may also experience some problems when upd can not receive the answer. There is no timeout concept for this in CF. You must implement it yourself.
Simply replace the P/Invoke methods with this:
[DllImport("kernel32.dll", EntryPoint = "SetLocalTime")]
static extern bool SetLocalTimeWin(ref SYSTEMTIME time);
[DllImport("coredll.dll", EntryPoint = "SetLocalTime")]
static extern bool SetLocalTimeCe(ref SYSTEMTIME time);
static bool SetLocalTime(ref SYSTEMTIME time)
{
if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
return SetLocalTimeWin(ref time);
return SetLocalTimeCe(ref time);
}
|
|
|
|
|
Really a great class 2 use.Good work buddy
btw how can i change the system time through code? bcoz as far as i know we can only read system time in c#
Vikas Salvi
Programmer Analyst
|
|
|
|
|
I agree! This is really handy code. As old as this article is, it is still very useful and exactly what I was looking for. A big 5 from me.
Vikas Salvi wrote: btw how can i change the system time through code?
It's right there in the code - the interop call to SetLocalTime()
QRZ? de WAØTTN
|
|
|
|