|
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
|
|
|
|
|
The SNTP C# code works on desktop PC. But while trying it on Pocket PC emulator am unable to receive any packets from the server.
Found that Active Sync does not support UDP.
Therefore, installed Virtual Machine Network Driver and VMWare. Yet the code does not work.
Please help me execute the code on Pocket PC. It is urgent.
rashmi chinchkar
|
|
|
|
|
This is a good article. I am developing a software that needs synchronization of time from server. But i dont see the way to get server time by using your class. Could you please tell me how?
I look forward to hearing from you.
Try
|
|
|
|
|
That is pretty simple. Read the manual
You need to instantiate the NTPClient class, then "connect" to the server. After that, have a look at how the ToString() method is implemented. Everything is there.
Delta Forth .NET (www.dataman.ro)
World's first Forth compiler for the .NET platform
|
|
|
|
|
Yeah, you are right. But to get server time is to call the method ReceiveTimestamp? I notice that both server and local time are the same. Or any other way to get server time?
|
|
|
|
|
Sorry for misunderstanding. I can manage things right now. Thank alot for your nice class and your reply.
Try
|
|
|
|
|
Hi All,
Can anybody provide me sample code in C++ for changing the client machine time from TIME SERVER?
This is very urgent..
Abhijit
|
|
|
|
|
Just wanted to say thanks for writing this. I think it will come in handy in my current project.
|
|
|
|
|
Firstly let me say that this is an excellent class an extremely useful.
That being said, I am encountering a small problem that hopefully somebody could shed some light on.
If I pass an IP address to the SNTPClient constructor it always returns succesful, even if the IP doesnt exist. As an example:
client = new SNTPClient("172.96.211.41");
client.Connect(true);
Console.Writeline(client.ToString());
Doesnt throw any exceptions and returns this
Leap Indicator: No warning
Version number: 3
Mode: Server
Stratum: Secondary Reference
Local time: 27/05/2005 16:29:39
Precision: 9.5367431640625E-07 s
Poll Interval: 1 s
Reference ID: skylar.fbamedia.co.uk (80.253.108.112)
Root Delay: 34.912109375 ms
Root Dispersion: 90.972900390625 ms
Round Trip Delay: 1 ms
Local Clock Offset: 0 ms
I have to admit, I have no idea where the reference ID is coming from.
If I use a non existant host name (e.g. foobar) it does throw an exception.
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
Hi!
Indeed a good article. Great job. I was searching for this functionality on net for quiet some time (to access a time server, as this is needed in my project).
I downloaded the source code and rebuilt it. But the app. is not running on my system. It throws an SocketException with Message : "No such Host is known" and Error code : 11001.
I tried to change the Time Server URL but with no succcess. The Exception is thrown while resolving the IP Address from URL in the line
IPHostEntry hostadd = Dns.Resolve(TimeServer);
of NTPClient.cs's Connect function.
Later I read about ur latest NTPClient app in the discussion board. But I'm facing the same problem.
I guess problem is that I'm using a proxy Server, and app is not able to bypass it. I tried to search ways to bypass the proxy server but again no success.
I would be really greatful to you if you could help me in solving this problem, as I need this functionality to access the Time Server to get the current time of any given location, for eg. London, France etc.
With Regards
and lots of thanks in advance
Shweta
|
|
|
|
|
Ask the sysadmin to open the port 123 for your machine. The same happens in my office. If I connect to Internet from home, the program works ok.
Delta Forth .NET (www.dataman.ro)
World's first Forth compiler for the .NET platform
|
|
|
|
|
This part of the code :
public double Precision<br />
{<br />
get<br />
{<br />
return (1000 * Math.Pow(2, NTPData[3]));<br />
}<br />
}
is wrong; it assumes the precision given in the received data is an unsigned byte instead of a signed byte which leads to abnormal results.
public double Precision<br />
{<br />
get<br />
{<br />
return (Math.Pow(2, (sbyte)NTPData[3]));<br />
}<br />
}
should work much better ![Smile | :)](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_smile.gif)
|
|
|
|
|
The issue was already addressed around September 2003, see the latest version at http://www.dataman.ro/sntp. Thanks for reminding me that I need to update the article on CP.
Delta Forth .NET (www.dataman.ro)
World's first Forth compiler for the .NET platform
|
|
|
|
|
I downloaded the source code and rebuilt it with .NET2003. It runs fine on my Win2000 machine but under WinXP Professional, I get the same exception "An existing connection was forcibly closed by the remote host" that somebody else mentioned. It's socket error code 10054 (WSACONNRESET) if that helps. Does anybody know what's going on?
|
|
|
|