|
-
Oct 30th, 2013, 08:54 AM
#1
Thread Starter
New Member
Difficulty connecting to a time server in vb.net
I am trying to use the following piece of code to connect to a time server and attain the time but have had no luck:
Dim ntpServer As String = "time.windows.com"
Dim ntpData(47) As Byte
Dim addresses = Dns.GetHostEntry(ntpServer).AddressList
Dim EndP As IPEndPoint = New IPEndPoint(addresses(0), 123)
Dim soc As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
soc.Connect(EndP)
soc.Send(ntpData)
soc.Receive(ntpData)
soc.Close()
Stepping through the program I can't get past the following line of code soc.Receive(ntpData). What am I doing wrong?
Thanks
-
Oct 30th, 2013, 09:30 AM
#2
Re: Difficulty connecting to a time server in vb.net
A couple things... I'm no expert on this so, take it for what it's worth...
1) ntpData is empty when you send it... don't you need to send something?
2 )I notice in the examples online for the Send and Recieve methods, they return an integer representing the number of bytes sent....
if you change your code similarly:
Code:
Dim ntpServer As String = "time.windows.com"
Dim ntpData(47) As Byte
Dim addresses = Dns.GetHostEntry(ntpServer).AddressList
Dim EndP As IPEndPoint = New IPEndPoint(addresses(0), 123)
Dim soc As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
soc.Connect(EndP)
Dim i as integer = soc.Send(ntpData)
'If you check i here... I bet it's zero because the array was empty.
'Since nothing was sent, the server doesn't know to send anything back... so the receive hangs...
i = soc.Receive(ntpData)
soc.Close()
As to what to send to the server in the first place? That I do not know.
Documentation & example: http://msdn.microsoft.com/en-us/library/w93yy28a.aspx
-tg
-
Oct 30th, 2013, 09:59 AM
#3
Thread Starter
New Member
Re: Difficulty connecting to a time server in vb.net
Hi and thanks for your reply. I kind of figured this was the issue. I am essentially trying to translate some c code into vb.net but am unsure of what to do with the following:
ntpData[0] = 0x1B; //LI = 0 (no warning), VN = 3 (IPv4 only), Mode = 3 (Client Mode)
Do you know what 0x1B is in vb.net?
-
Oct 30th, 2013, 10:02 AM
#4
Thread Starter
New Member
Re: Difficulty connecting to a time server in vb.net
Also i attains a value of 48 (ntpData is an array of length 48 containing zeros)
-
Oct 30th, 2013, 12:26 PM
#5
Re: Difficulty connecting to a time server in vb.net
Basically you sent 48 bytes of nothing... so it's not going to respond...
0x1B is hex... translates to 27 in decimal... Hex is represented in VB by using &H ... so &H1B is the equivalent... you can also just use 27, it's the samething.
Code:
data(0) = &H1B
data(1) = 27
So it looks like you need to sent byte 27 in order to provoke a response back from the server.
-tg
-
Oct 30th, 2013, 01:10 PM
#6
Re: Difficulty connecting to a time server in vb.net
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|