Click here to Skip to main content
11,922,641 members (57,060 online)
Click here to Skip to main content
Add your own
alternative version


29 bookmarked

Connecting To A Network Time Server

, 11 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
This simple article explains how to connect to a network time server


This article is a very simple introduction to the Network Time Protocol. It explains briefly how you can synchronize 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 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 uses UDP on port 123.

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

    TcpClient NTS = new TcpClient("", 13); 

    if (NTS.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]; 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Syed M Hussain
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

Questionsalam Pin
arastoooo5-Sep-11 14:15
memberarastoooo5-Sep-11 14:15 
GeneralAlternative approach Pin
djlove14-Jul-08 23:19
memberdjlove14-Jul-08 23:19 
GeneralI updated your TimeServer code Pin
PawJershauge23-Sep-07 23:40
memberPawJershauge23-Sep-07 23:40 
GeneralRe: I updated your TimeServer code Pin
Lechuss24-Sep-07 22:19
memberLechuss24-Sep-07 22:19 
GeneralRe: I updated your TimeServer code Pin
cspinelive4-Aug-08 11:42
membercspinelive4-Aug-08 11:42 
thanks. This worked well except for one thing. I had to change the "ddd MMM d HH:mm:ss yyyy" to "ddd MMM  d HH:mm:ss yyyy" (added an additional space after MMM). It was August 4th when I did this. I hope it doesn't break when the day portion of the date becomes two digits.
GeneralMinor error Pin
narsyseth5-Mar-07 5:04
membernarsyseth5-Mar-07 5:04 
GeneralRe: Minor error Pin
Syed M Hussain5-Mar-07 5:55
memberSyed M Hussain5-Mar-07 5:55 
GeneralRe: Minor error Pin
narsyseth5-Mar-07 5:57
membernarsyseth5-Mar-07 5:57 
QuestionWhy don't use DateTime.Parse? Pin
Christian Klauser4-Mar-07 9:54
memberChristian Klauser4-Mar-07 9:54 
AnswerRe: Why don't use DateTime.Parse? Pin
PawJershauge23-Sep-07 23:25
memberPawJershauge23-Sep-07 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151125.1 | Last Updated 11 Jul 2008
Article Copyright 2007 by Syed M Hussain
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid