In C#, how do I query a remote server for its current time?
Similar functionality to
net time \\servername
but returning a datestamp that includes seconds.
Thanks
In C#, how do I query a remote server for its current time?
Similar functionality to
net time \\servername
but returning a datestamp that includes seconds.
Thanks
You can try getting the daytime on port 13:
System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient ("yourmachineHOST", 13);
System.IO.StreamReader rd = new System.IO.StreamReader (t.GetStream ());
Console.WriteLine (rd.ReadToEnd ());
rd.Close();
t.Close();
You can use the NetRemoteTOD function.
An example from http://bytes.com/groups/net-c/246234-netremotetod-usage:
// The pointer.
IntPtr pintBuffer = IntPtr.Zero;
// Get the time of day.
int pintError = NetRemoteTOD(@"\\sony_laptop", ref pintBuffer);
// Get the structure.
TIME_OF_DAY_INFO pobjInfo = (TIME_OF_DAY_INFO)
Marshal.PtrToStructure(pintBuffer, typeof(TIME_OF_DAY_INFO));
// Free the buffer.
NetApiBufferFree(pintBuffer);
Windows Time Service implements NTP. Here is a C# implementation of an NTP client.