Skip to main content

Past Blast

Featured Products

Windows Mobile Developer Controls
Windows Mobile Developer Controls
Stay in touch using the DEVBUSS RSS feeds.
 

News

Windows Mobile Developer Controls
Windows Mobile Developer Controls

Using NTP to set the time on your Pocket PC

Written by Pete Vickers  [author's bio]  [read 52868 times]
Edited by Derek

Download the code

Page 1  Page 2  Page 3 

I was talking to someone who needed the correct time on their Pocket PC for deliveries etc and needed to access a time server. There are many 'time servers' that can be accessed on the internet which will give you the correct time using NTP (Network Time Protocol). Quite difficult to do in VB6, never mind eVB, but undaunted, we will continue. If you take a look at the services file on your PC (c:\winnt\system32\drivers\etc\services), you will see an entry:-

time 37/tcp timserver

This tells us we can get to the timeserver by using port 37. Try to 'Telnet' in to port 37 on your PC (alternatively try "telnet pogostick.net 13"). Chances are the connection will be refused. Not so undaunted now. End of article - it doesn't work. But hang on, if we take a closer look at the services file, we find at number 13 in the charts...

daytime 13/tcp

Just try 'Telnet' to port 13 on your PC. You will get disconnected fairly smartly, but this time, you will get something back from your PC. What you will get back is a time and date. Not in a useable format, but what's life without a challenge.

So, lets get down to cutting some code. Fire up eVB, and scatter randomly on the form, 2 command buttons, 1 label, a combo box, a listbox and just for good measure, add a winsock control as you may need it later. Don't worry about the positioning, we can always do it in code. Don't worry about the names, leave them as default. Hell, we're programming, naming standard are for wimps.

You should now have a form that looks somewhat like this...

The command buttons are going to allow us to connect and disconnect. The combo box will list our time servers, and the listbox will give us an indication of what is happening behind the scenes.

Next, declare some form variables and API calls.

Option Explicit
Private sysTime As String
Private iyear As Integer
Private imonth As Integer
Private idow As Integer
Private iday As Integer
Private ihour As Integer
Private imin As Integer
Private isec As Integer
Private imill As Integer
Private Rbuff As String
Private bConnected As Boolean
Private lret As Long
Const IDC_WAIT = 32514
Declare Function SetLocalTime Lib "Coredll" _
(ByVal lpSystemTime As String) As Long
Declare Function LoadCursor Lib "Coredll" _
Alias "LoadCursorW" _
(ByVal hInstance As Long, _
ByVal lpCursorName As Long) As Long
Declare Function SetCursor Lib "Coredll" _
(ByVal hCursor As Long) As Long

Next the form_load procedure

Private Sub Form_Load()
Command1.Move 120, 120, 1335, 495
Command2.Move 120, 720, 1335, 495
Label1.Move 1560, 120, 1695, 255
Combo1.Move 120, 1700, 3000, 300
list1.Move 120, 2200, 3000, 1000
Command1.Caption = "Connect"
Command2.Caption = "Close Winsock"
Command2.Enabled = False
Label1.Caption = ""
Combo1.AddItem "192.192.192.46" 'your server
Combo1.AddItem "ntp.ise.canberra.edu.au"
...
Combo1.listindex = 0

We are adding lots of servers to the combo box, but they are all contained in the sample code so we won't add them all now.

Next Page