Access Network Time Protocol (NTP) Server
Author
Ernst H.K. Stelzer
Title
Access Network Time Protocol (NTP) Server
Description
Use UDP services from various NTP servers to get exact time including Dynamic NTP Access
Category
Educational Materials
Keywords
NETLink, UDP services, .NET, external services, time and date, Windows services
URL
http://www.notebookarchive.org/2019-12-3pi43da/
DOI
https://notebookarchive.org/2019-12-3pi43da
Date Added
2019-12-08
Date Last Modified
2019-12-08
File Size
124.6 kilobytes
Supplements
Rights
CC BY 4.0
This notebook has not been updated since 2019.
Download
Open in Wolfram Cloud
Access Network Time Protocol (NTP) Server
Access Network Time Protocol (NTP) Server
Use UDP services from various NTP servers to get exact time
Ernst H.K. Stelzer, Goethe-Universität Frankfurt am Main, January 2012 - December 2019
Initializations
Initializations
In[]:=
gridOpts={Alignment{Left,Center},ItemSizeFull,FrameAll,BackgroundLightBlue};
In[]:=
HyperlinkButton[text_,url_]:=Hyperlink[Button[Style["Hyperlink: "<>text,"Subsection",Black]],url]
References
References
UDP NTP Access
UDP NTP Access
https://forum.arduino.cc/index.php?PHPSESSID=ht6thhm49pa2fji8gij90iu733&topic=197637.msg1458539#msg1458539
NTP & DST handling
NTP & DST handling
http://www.webexhibits.org/daylightsaving/b2.html
In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time). It begins the last Sunday in March and ends the last Sunday in October. In the EU, all time zones change at the same moment.
In the European Union, Summer Time begins and ends at 1:00 a.m. Universal Time (Greenwich Mean Time). It begins the last Sunday in March and ends the last Sunday in October. In the EU, all time zones change at the same moment.
2019 March 31 October 272020 March 29 October 252021 March 28 October 312022 March 27 October 30
In[]:=
LocalTime[]
Out[]=
Sun 8 Dec 2019 14:08:45GMT+1.
In[]:=
LocalTimeZone[]
Out[]=
Europe/Berlin
In[]:=
Needs["NETLink`"]
In[]:=
InstallNET[]
Out[]=
LinkObject
|
In[]:=
myPadding[x_]:=If[x≥100,PaddedForm[x,{5,2},NumberPadding{"","0"}],PaddedForm[x,{4,2},NumberPadding{"0","0"}]]
NTP interpreted values
NTP interpreted values
In[]:=
leapIndicatorList={"No warning","Last minute of day has 61 sec","Last minute of day has 59 sec","Alarm"};modeList={"Unknown","Symmetric Active","Symmetric Passive","Client","Server","Broadcast","NTP control message"};stratumList={"Unspecified","Primary Server","Secondary Server","Unsynchronized","Reserved"};
In[]:=
(*ntpServer="64.90.182.55";(*NISTserver,NY,USA*)*)
In[]:=
ntpServer="192.53.103.108";(*PTBserverBraunschweigGermany*)
In[]:=
ntpServer="pool.ntp.org";(*World-widepool*)
In[]:=
ntpServer="europe.pool.ntp.org";(*Europe—europe.pool.ntp.org*)
In[]:=
ntpServer="2.europe.pool.ntp.org";(*OneoffourserversinEurope—europe.pool.ntp.org*)
Prepares the NTP UDP request package. which consist of 48 bytes.
In[]:=
packetLength=48;packet=Table[0,{packetLength}];packet[[1;;4]]={2^^11100011,10^^0,10^^6,16^^EC};packet[[13;;16]]={10^^49,16^^4E,10^^49,10^^52};
In[]:=
sendPacket=NETNew["System.Byte[]",packetLength];sendPacket=packet;receivePacket=NETNew["System.Byte[]",packetLength];
In[]:=
udp=NETNew["System.Net.Sockets.UdpClient"];endPoint=NETNew["System.Net.IPEndPoint",1,1];
In[]:=
udp@Connect[ntpServer,123]
In[]:=
err=udp@Send[sendPacket,packetLength]
Out[]=
48
In[]:=
available=udp@Available[]
Out[]=
0
In[]:=
broadcatsQ=udp@EnableBroadcast[];
In[]:=
ttlNumber=udp@Ttl[];
In[]:=
receivePacket=udp@Receive[endPoint];
In[]:=
endPoint@Address@ToString[]
Out[]=
193.182.111.142
In[]:=
endPoint@ToString[]
Out[]=
193.182.111.142:123
In[]:=
NETObjectToExpression[endPoint@ToString[]]
Out[]=
193.182.111.142:123
In[]:=
dataPacket=NETObjectToExpression[receivePacket]
Out[]=
{36,2,6,236,0,0,0,72,0,0,6,143,192,36,143,130,225,151,113,176,102,41,195,44,0,0,0,0,0,0,0,0,225,151,116,94,172,64,160,89,225,151,116,94,172,64,198,16}
In[]:=
leapIndicator=BitShiftRight[BitAnd[dataPacket[[1]],2^^11000000],6]
Out[]=
0
In[]:=
versionNumber=BitShiftRight[BitAnd[dataPacket[[1]],2^^00111000],3]
Out[]=
4
In[]:=
mode=BitAnd[dataPacket[[1]],2^^00000111]
Out[]=
4
In[]:=
stratum=dataPacket[[2]]
Out[]=
2
In[]:=
stratumText=Which[stratum==0,stratumList[[1]],stratum1,stratumList[[2]],2≤stratum≤15,stratumList[[3]],stratum16,stratumList[[4]],stratum>16,stratumList[[5]]]
Out[]=
Secondary Server
In[]:=
poll=If[dataPacket[[3]]>127,-(255-dataPacket[[3]]),dataPacket[[3]]]
Out[]=
6
In[]:=
pollValue=2^poll
Out[]=
64
In[]:=
precision=If[dataPacket[[4]]>127,-(255-dataPacket[[4]]),dataPacket[[4]]]
Out[]=
-19
In[]:=
precisionValue=1000.02^precision
Out[]=
0.00190735
In[]:=
rootDelay=dataPacket[[5;;8]]
Out[]=
{0,0,0,72}
In[]:=
rootDelayValue=1000.0(Fold[256#1+#2&,0,dataPacket[[5;;6]]]+Fold[256#1+#2&,0,dataPacket[[7;;8]]]/2^16)
Out[]=
1.09863
In[]:=
rootDispersion=dataPacket[[9;;12]]
Out[]=
{0,0,6,143}
In[]:=
rootDispersionValue=1000.0(Fold[256#1+#2&,0,dataPacket[[9;;10]]]+Fold[256#1+#2&,0,dataPacket[[11;;12]]]/2^16)
Out[]=
25.6195
In[]:=
refIdentifier=dataPacket[[13;;16]]
Out[]=
{192,36,143,130}
In[]:=
refIdentifierString=If[Fold[And,True,(64<#<123||#0)&/@refIdentifier],FromCharacterCode[refIdentifier],ToString[dataPacket[[13]]]<>"."<>ToString[dataPacket[[14]]]<>"."<>ToString[dataPacket[[15]]]<>"."<>ToString[dataPacket[[16]]]]
Out[]=
192.36.143.130
In[]:=
refTimestamp=Fold[256#1+#2&,0,dataPacket[[17;;20]]]
Out[]=
3784798640
In[]:=
refTimestampMS=1000.0Fold[256#1+#2&,0,dataPacket[[21;;24]]]/2^32
Out[]=
399.075
In[]:=
orgTimestamp=Fold[256#1+#2&,0,dataPacket[[25;;28]]]
Out[]=
0
In[]:=
orgTimestampMS=1000.0Fold[256#1+#2&,0,dataPacket[[29;;32]]]/2^32
Out[]=
0.
In[]:=
rcvTimestamp=Fold[256#1+#2&,0,dataPacket[[33;;36]]]
Out[]=
3784799326
In[]:=
rcvTimestampMS=1000.0Fold[256#1+#2&,0,dataPacket[[37;;40]]]/2^32
Out[]=
672.861
In[]:=
trmTimestamp=Fold[256#1+#2&,0,dataPacket[[41;;44]]]
Out[]=
3784799326
In[]:=
trmTimestampMS=1000.0Fold[256#1+#2&,0,dataPacket[[45;;48]]]/2^32
Out[]=
672.863
In[]:=
DateString[trmTimestamp,TimeZone2]
Out[]=
Sun 8 Dec 2019 14:08:46
In[]:=
endPoint@ToString[]
Out[]=
193.182.111.142:123
In[]:=
Grid[{{"Descriptions","NTP returned value","NTP interpreted value"},{"End point",ntpServer,endPoint@ToString[]},{"Leap Indicator",leapIndicator,leapIndicatorList[[leapIndicator+1]]},{"Version Number",versionNumber,versionNumber},{"Mode",mode,modeList[[mode+1]]},{"Stratum",stratum,stratumText},{"Poll Interval",poll,pollValue},{"Precision",precision,precisionValue" ms"},{"Root Delay",rootDelay,rootDelayValue" ms"},{"Root Dispersion",rootDispersion,rootDispersionValue" ms"},{"Reference Identifier",refIdentifier,refIdentifierString},{"Reference Timestamp",refTimestamp,DateString[refTimestamp]<>" (+ "<>ToString[refTimestampMS//myPadding]<>" ms)"},{"Originate Timestamp",orgTimestamp,DateString[orgTimestamp]<>" (+ "<>ToString[orgTimestampMS//myPadding]<>" ms)"},{"Receive Timestamp",rcvTimestamp,DateString[rcvTimestamp]<>" (+ "<>ToString[rcvTimestampMS//myPadding]<>" ms)"},{"Transmit Timestamp",trmTimestamp,DateString[trmTimestamp]<>" (+ "<>ToString[trmTimestampMS//myPadding]<>" ms)"}},FrameAll,ItemStyleDirective[FontFamily"Courier",FontSize14],Alignment{{Right,Right,Right},Top},Background{None,{Lighter[Yellow,.9],{White,Lighter[Blend[{Blue,Green}],.8]}}},Dividers{{},Table[(2+5n)Thickness[2],{n,0,4}]},Spacings{5,1}]
Out[]=
Descriptions | NTP returned value | NTP interpreted value |
End point | 2.europe.pool.ntp.org | 193.182.111.142:123 |
Leap Indicator | 0 | No warning |
Version Number | 4 | 4 |
Mode | 4 | Server |
Stratum | 2 | Secondary Server |
Poll Interval | 6 | 64 |
Precision | -19 | 0.00190735 ms |
Root Delay | {0,0,0,72} | 1.09863 ms |
Root Dispersion | {0,0,6,143} | 25.6195 ms |
Reference Identifier | {192,36,143,130} | 192.36.143.130 |
Reference Timestamp | 3784798640 | Sun 8 Dec 2019 12:57:20 (+ 399.07 ms) |
Originate Timestamp | 0 | Mon 1 Jan 1900 00:00:00 (+ 000.00 ms) |
Receive Timestamp | 3784799326 | Sun 8 Dec 2019 13:08:46 (+ 672.86 ms) |
Transmit Timestamp | 3784799326 | Sun 8 Dec 2019 13:08:46 (+ 672.86 ms) |
Dynamic NTP Access
Dynamic NTP Access
In[]:=
Dynamic[{trmTimestamp,DateString[trmTimestamp,TimeZone(2+2)],trmTimestampMS//myPadding,counter1,counter2,av}]
Out[]=
{trmTimestamp,DateString[trmTimestamp,TimeZone4],
myPadding[trmTimestampMS],counter1,counter2,av}
In[]:=
stop=False;counter1=counter2=0;Button["Stop",stop=True]While[!stop,counter1++;err=udp@Send[sendPacket,packetLength];While[(av=udp@Available[])>0,counter2++;receivePacket=udp@Receive[endPoint];dataPacket=NETObjectToExpression[receivePacket];trmTimestamp=Fold[256#1+#2&,0,dataPacket[[41;;44]]];trmTimestampMS=1000.0Fold[256#1+#2&,0,dataPacket[[45;;48]]]/2^32;];Pause[1];]
Out[]=
Stop
In[]:=
udp@Close[]
In[]:=
ReleaseNETObject[udp]ReleaseNETObject[sendPacket]ReleaseNETObject[receivePacket]ReleaseNETObject[endPoint]
In[]:=
UninstallNET[]
Out[]=
C:\Program Files\Wolfram Research\Mathematica\12.0\SystemFiles\Links\NETLink\InstallableNET.exe
Information on System.Net.Sockets.UdpClient
Information on System.Net.Sockets.UdpClient
In[]:=
NETTypeInfo[udp]
::arg1
In[]:=
NETTypeInfo["System.Net.IPEndPoint"]
● Type | ||||
class System.Net.IPEndPoint | ||||
| ||||
Interfaces Implemented: None | ||||
Assembly-Qualified Name: System.Net.IPEndPoint, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | ||||
Assembly Location: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll | ||||
| ||||
● Constructors | ||||
IPEndPoint(long address, int port) | ||||
IPEndPoint(System.Net.IPAddress address, int port) | ||||
| ||||
● Fields | ||||
const int MaxPort | ||||
const int MinPort | ||||
| ||||
● Properties | ||||
System.Net.IPAddress Address | ||||
override System.Net.Sockets.AddressFamily AddressFamily [read only] | ||||
int Port | ||||
| ||||
● Methods | ||||
override System.Net.EndPoint Create(System.Net.SocketAddress socketAddress) | ||||
override bool Equals(object comparand) | ||||
static bool Equals(object objA, object objB) | ||||
override int GetHashCode() | ||||
Type GetType() | ||||
static bool ReferenceEquals(object objA, object objB) | ||||
override System.Net.SocketAddress Serialize() | ||||
override string ToString() |
In[]:=
NETTypeInfo["System.Net.IPAddress"]
● Type | |||
class System.Net.IPAddress | |||
| |||
Interfaces Implemented: None | |||
Assembly-Qualified Name: System.Net.IPAddress, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 | |||
Assembly Location: C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll | |||
| |||
● Constructors | |||
IPAddress(long newAddress) | |||
IPAddress(byte[] address, long scopeid) | |||
IPAddress(byte[] address) | |||
| |||
● Fields | |||
static readonly System.Net.IPAddress Any | |||
static readonly System.Net.IPAddress Broadcast | |||
static readonly System.Net.IPAddress IPv6Any | |||
static readonly System.Net.IPAddress IPv6Loopback | |||
static readonly System.Net.IPAddress IPv6None | |||
static readonly System.Net.IPAddress Loopback | |||
static readonly System.Net.IPAddress None | |||
| |||
● Properties | |||
long Address | |||
System.Net.Sockets.AddressFamily AddressFamily [read only] | |||
bool IsIPv4MappedToIPv6 [read only] | |||
bool IsIPv6LinkLocal [read only] | |||
bool IsIPv6Multicast [read only] | |||
bool IsIPv6SiteLocal [read only] | |||
bool IsIPv6Teredo [read only] | |||
long ScopeId | |||
| |||
● Methods | |||
override bool Equals(object comparand) | |||
static bool Equals(object objA, object objB) | |||
byte[] GetAddressBytes() | |||
override int GetHashCode() | |||
Type GetType() | |||
static long HostToNetworkOrder(long host) | |||
static int HostToNetworkOrder(int host) | |||
static short HostToNetworkOrder(short host) | |||
static bool IsLoopback(System.Net.IPAddress address) | |||
System.Net.IPAddress MapToIPv4() | |||
System.Net.IPAddress MapToIPv6() | |||
static long NetworkToHostOrder(long network) | |||
static int NetworkToHostOrder(int network) | |||
static short NetworkToHostOrder(short network) | |||
static System.Net.IPAddress Parse(string ipString) | |||
static bool ReferenceEquals(object objA, object objB) | |||
override string ToString() | |||
static bool TryParse(string ipString, out System.Net.IPAddress address) |
Copyright & Version
Copyright & Version
© Ernst H.K. Stelzer, 2012-2020.
ernst.stelzer@physikalischebiologie.de
Version 2.1, December 2019. Please do not hesitate to send me your comments and suggestions.
Physical Biology (FB 15, IZN), www.physikalischebiologie.de
Buchmann Institute for Molecular Life Sciences (CEF-MC)
Goethe-Universität Frankfurt am Main (Campus Riedberg)
Max-von-Laue-Straße 15, D-60438 Frankfurt am Main, Germany
Tel +49 (69) 798 42547, x42545, Fax +49 (69) 798 42546
ernst.stelzer@physikalischebiologie.de
Version 2.1, December 2019. Please do not hesitate to send me your comments and suggestions.
Physical Biology (FB 15, IZN), www.physikalischebiologie.de
Buchmann Institute for Molecular Life Sciences (CEF-MC)
Goethe-Universität Frankfurt am Main (Campus Riedberg)
Max-von-Laue-Straße 15, D-60438 Frankfurt am Main, Germany
Tel +49 (69) 798 42547, x42545, Fax +49 (69) 798 42546
Cite this as: Ernst H.K. Stelzer, "Access Network Time Protocol (NTP) Server" from the Notebook Archive (2019), https://notebookarchive.org/2019-12-3pi43da
Download