|
De ce furnizezi aici niste informatii militare? Incearca sa schimbi putin lucrurile pe aici.
|
|
|
|
|
How to "stop code" if Server blocked..? Thanks
|
|
|
|
|
Why is mode name 'Server'? Shouldnt it be "client"?
?
<pre> private void Initialize()
{
NTPData[0] = 0x1B;
for (int i = 1; i < 48; i++)
{
NTPData[i] = 0;
}
TransmitTimestamp = DateTime.Now;
}
|
|
|
|
|
while (!messageReceived && (elapsedTime < TimeOutInterval))
{
sendSocket.SendTo(SNTPData, SNTPData.Length, SocketFlags.None, sendEP);
if (sendSocket.Available > 0)
{
messageLength = sendSocket.ReceiveFrom(SNTPData, ref epSendEP);
if (!IsResponseValid())
{
throw new Exception("Invalid response from " + TimeServer);
}
messageReceived = true;
break;
}
Thread.Sleep(1000);
elapsedTime += 1000;
}
With this sleep you drastically increase server -> client time and DestinationTimestamp in the end.
So we have wrong RoundTripDelay and most importantly LocalClockOffset.
Best solution: Socket timeout properties with Send and Receive (will not work in compact framework). Or at least use Thread.Sleep(1).
P.s. I think we need to use all this synchronization with TimeBeginPeriod(1), in other case the precision will be far less then 15 ms...
modified 16-Mar-16 14:23pm.
|
|
|
|
|
Great little project. I have been using the code for ages. Now updating my app in VS 2013. Using your latest code as suggested to another reader. The new code in Connect reads:
IPEndPoint listenEP = new IPEndPoint(IPAddress.Any, 123);
Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPHostEntry hostEntry = Dns.GetHostEntry(TimeServer);
IPEndPoint sendEP = new IPEndPoint(hostEntry.AddressList[0], 123);
EndPoint epSendEP = (EndPoint)sendEP;
int messageLength = 0;
try
{
sendSocket.Bind(listenEP);
The Bind gives an error:
Only one usage of each socket address (protocol/network address/port) is normally permitted
I have googled the error. The suggested changes to the Registry had already been done. My development machine is Win 7 Pro 64bit.
Can anyone help me understand and solve the problem?
|
|
|
|
|
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
|
|
|
|