Connecting To A Network Time Server


Download

This article is a very simple introduction to the Network Time Protocol. It explains briefly how you can synchronise your applications with a time from a Network Time Server. NTP provides a reliable way of synchronizing time on IT networks. NTP is present on virtually all computers and allows systems to synchronize their clocks with a time source over the TCP/IP networks.

On a Windows operating system you can synchronize your system clock with time.windows.com. Alternatively you can enter a Network Time Server of your choice.

There are Network Time Servers all around the world, some are publicly accessible and are mainly setup in university institutions. Network Time Servers are mainly over UDP listening on port 123. Microsoft's Internet Time server time.windows.com uses UDP on port 123.

The following code connects to www.pogostick.net on port 13. This Network Time Server uses TCP. The server is located in Norway.


try
{
TcpClient NTS = new TcpClient("www.pogostick.net", 13);

if (NTS.Connected)
{
//Connected
}

}
catch (Exception E)
{
//Not Connected
}


Once connected to the Network Time Server, the server will respond with the date and time. The following code sets up a NetworkStream and a StreamReader to read data from the server.


NetworkStream ns = NTS.GetStream();
StreamReader sr = new StreamReader(ns);
string Response = sr.ReadLine();


The server will respond with a message like the following.


Sun Mar 3 22:04:24 2007


We can extract the time from the response by using the split method. The following code splits the response from the server and stores the data into an array variable.


static void ProcessResponse()
{
string[] splitRes = Response.Split(' ');
strDate = splitRes[0];
strMonth = splitRes[1];
intDate = int.Parse(splitRes[3]);
strTime = splitRes[4];
strYear = splitRes[5];
}



1 Simple DataGridView Example - C#
This example demonstrates how to load data into the DataGridView control using string array's.
2 A Custom Message Box - C#
This example source code demonstrates how you can develop your own messages box.
3 Custom MessageBox 2 - C#
This example source code builds on a previous example of creating a custom MessageBox. New features in this example include different types of buttons and displaying an icon. Also uses different message beep tones for standard MessageBox's and warring MessageBox's.
4 Connect To MSN Messenger Using The MSN Protocol - C#
This sample application demonstrates how to connect to MSN Messenger using the MSN Protocol. It also includes how to generate a Ticket that is used with the ChallengeString. This is a simple authentication example.
5 Send Messages Using The Net Send Command - C#
This sample program demonstrates how to send messages in a network using the Windows Messenger service.
1 Transfer data from CSV file to MySQL - Java
This snippet transfers deliminated data from a CSV file to a MySQL database. You need to edit the code to include your database connection details and also the fields from the CSV and MySQL needs to correspond with your fields.
2 Read Image File As bytes - C#
This sinppet opens an image for reading as bytes. The bytes are then placed into a memorystream which is used to create a bitmap image of the bytes. The image is then set as the forms background image.
3 Retrieve the path of a file, using openfiledialog - C#
This snippet uses the openfiledialog class to get a files path. Useful for opening files from a location on the harddrive.
4 Load web image into picturebox - Visual Basic.Net
Function with picturebox and url as argument to display the passed url to image inside a picturebox I got this from freevbcode a while back.
5 Random Number Generator - Visual Basic.Net
Function that reurns a random non-repeating integer
CY2 Online2.net | CopyRight 2005 - 2008 | All Rights Reserved