Results 1 to 4 of 4

Thread: [RESOLVED] Socket Receive

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Resolved [RESOLVED] Socket Receive

    Hi everyone

    I have a little problem with a socket that i'm using to get the current date and time for an serve, i can send the request, but i don't receive anything.

    The current code:
    vb.net Code:
    1. Dim sock As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
    2.         Dim ip As Net.IPHostEntry = Net.Dns.GetHostEntry("time-a.nist.gov")
    3.         Dim iep As New Net.IPEndPoint(ip.AddressList(0), 123)
    4.  
    5.         sock.Connect(iep)
    6.         If sock.IsBound Then Debug.WriteLine("Connected...")
    7.  
    8.         sock.SendTimeout = 3000
    9.         sock.ReceiveTimeout = 15000
    10.         Dim bytesSent As Integer = sock.Send(SNTPData)
    11.         Dim bytesRec As Integer = sock.Receive(SNTPData)

    The SNTPData has the correct size and the correct format.

    The timeout and the error:
    A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
    What i'm missing here?

    THanks

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    Re: Socket Receive

    Solved
    Use another version of the Receive method.

    vb.net Code:
    1. Dim bytesRec As Integer = sock.ReceiveFrom(SNTPData, iep)

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [RESOLVED] Socket Receive

    So, you get as far as send, but then the error occurs? Does it take you to the Send line such that the Receive line is never reached?

    Frankly, I don't think the Receive line is all that good. If Send doesn't block, the Receive line will be executed pretty much immediately, before even a fast reply could arrive well. The socket should raise an event that you can handle, though to be fair, I have only worked with UDP lately, which isn't quite the same. In any case, the error doesn't sound consistent with this issue anyways. You are getting a timeout, which I would not expect to happen with the Receive call.
    My usual boring signature: Nothing

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: [RESOLVED] Socket Receive

    He could have simply set ReceiveTimeOut to 0 which would block until the socket actually received data. His code is throwing that exception because that's that the time out is supposed to do.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

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