Results 1 to 6 of 6

Thread: Difficulty connecting to a time server in vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    9

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    9

    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?

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2013
    Posts
    9

    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)

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Difficulty connecting to a time server in vb.net

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width