Click here to Skip to main content
Licence CPOL
First Posted 3 Mar 2007
Views 30,112
Downloads 314
Bookmarked 26 times

Connecting To A Network Time Server

By | 11 Jul 2008 | Article
This simple article explains how to connect to a network time server

Introduction

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 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]; 
}

License

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



United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionsalam Pinmemberarastoooo13:15 5 Sep '11  
GeneralAlternative approach Pinmemberdjlove22:19 14 Jul '08  
GeneralI updated your TimeServer code PinmemberPawJershauge22:40 23 Sep '07  
GeneralRe: I updated your TimeServer code PinmemberLechuss21:19 24 Sep '07  
GeneralRe: I updated your TimeServer code Pinmembercspinelive10:42 4 Aug '08  
GeneralMinor error Pinmembernarsyseth4:04 5 Mar '07  
GeneralRe: Minor error PinmemberSyed M Hussain4:55 5 Mar '07  
GeneralRe: Minor error Pinmembernarsyseth4:57 5 Mar '07  
QuestionWhy don't use DateTime.Parse? PinmemberChristian Klauser8:54 4 Mar '07  
AnswerRe: Why don't use DateTime.Parse? PinPopularmemberPawJershauge22:25 23 Sep '07  

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

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

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