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 use NTP protocol to retrieve datetime from a remote server.
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With