Results 1 to 3 of 3

Thread: UDP Receive Asynchronously

  1. #1

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    UDP Receive Asynchronously

    I am trying to asynchronously receive UDP commands from my other program. I am using the code below which works great, once. But if i resend the command, it doesn't receive it.

    How do I continuously receive UDP packets?

    vb.net Code:
    1. Private Sub startListening()
    2.         ReceivingUdpClient = New System.Net.Sockets.UdpClient(listenPort)
    3.         ThreadReceive = New System.Threading.Thread(AddressOf receiveMessages)
    4.         ThreadReceive.Start()
    5.     End Sub
    6.  
    7.     Private Sub receiveMessages()
    8.         Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(RemoteIpEndPoint)
    9.         MsgBox(Encoding.ASCII.GetString(receiveBytes))
    10.         'the end goal is a Case but message box for debugging
    11.     End Sub
    My usual boring signature: Something

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: UDP Receive Asynchronously

    Since the Receive method blocks until something arrives and the fact that it's in a worker thread, I believe all you need to do is wrap the receive in a do loop. I'm still kind of learning this stuff, so if nobody posts saying I'm not right, it means I'm learning real good.

    VB Code:
    1. Private Sub receiveMessages()
    2. Do        
    3. Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(RemoteIpEndPoint)
    4.         MsgBox(Encoding.ASCII.GetString(receiveBytes))
    5.         'the end goal is a Case but message box for debugging
    6. Loop
    7.     End Sub
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    WiggleWiggle dclamp's Avatar
    Join Date
    Aug 2006
    Posts
    3,527

    Re: UDP Receive Asynchronously

    That did it! Thanks!
    My usual boring signature: Something

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