Results 1 to 4 of 4

Thread: udp send and receive

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    5

    udp send and receive

    hi all, i have a program that needs bi directional comunication with a device i bought,
    i can send udp commands to it, thats working like a charm, but when i try to receive data from it i can only see the data that im sending, not the response,
    my code:
    Code:
     Public udpClient As New UdpClient(11000)
     Public udpClient2 As New UdpClient(52380)
     Private Sub Hoofd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            udpClient.Connect("255.255.255.255", 52380)
            udp_scan.Start()
            udpClient2.Client.ReceiveTimeout = 1000
        End Sub
    
    Private Sub udp_scan_Tick(sender As Object, e As EventArgs) Handles udp_scan.Tick
            info_label.Text = "ZOEKEN..."
            Dim sendbytes = New Byte() {&H2, &H45, &H4E, &H51, &H3A, &H6E, &H65, &H74, &H77, &H6F, &H72, &H6B, &HFF, &H3}
            udpClient.Send(sendbytes, sendbytes.Length)
            doread()
        End Sub
    
    Function doread()
            Try
                Dim iepRemoteEndPoint As IPEndPoint = New _
                IPEndPoint(IPAddress.Any, 52380)
                Dim strMessage As String = String.Empty
                Do
    
    
                    Dim bytRecieved As Byte() =
                   udpClient2.Receive(iepRemoteEndPoint)
                    strMessage = Encoding.ASCII.GetString(bytRecieved)
    
                    Console.WriteLine("This is the message you received: " _
                   + strMessage)
    
                Loop While (strMessage <> "exit")
                udpClient2.Close()
    
    
            Catch e As Exception
    
                Console.WriteLine(e.ToString())
            End Try
            Return 0
        End Function

    in wireshark i can see both the data i send and the response, in my program i only receive what im sending not the response.
    i added the full project in attachments
    Who can help me ?
    Last edited by Shaggy Hiker; Oct 18th, 2020 at 09:09 AM. Reason: Added CODE tags.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: udp send and receive

    Please don't post unformatted code snippets. They are too hard to read.
    vb.net Code:
    1. Public udpClient As New UdpClient(11000)
    2.  Public udpClient2 As New UdpClient(52380)
    3.  Private Sub Hoofd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         udpClient.Connect("255.255.255.255", 52380)
    5.         udp_scan.Start()
    6.         udpClient2.Client.ReceiveTimeout = 1000
    7.     End Sub
    8.  
    9. Private Sub udp_scan_Tick(sender As Object, e As EventArgs) Handles udp_scan.Tick
    10.         info_label.Text = "ZOEKEN..."
    11.         Dim sendbytes = New Byte() {&H2, &H45, &H4E, &H51, &H3A, &H6E, &H65, &H74, &H77, &H6F, &H72, &H6B, &HFF, &H3}
    12.         udpClient.Send(sendbytes, sendbytes.Length)
    13.         doread()
    14.     End Sub
    15.  
    16. Function doread()
    17.         Try
    18.             Dim iepRemoteEndPoint As IPEndPoint = New _
    19.             IPEndPoint(IPAddress.Any, 52380)
    20.             Dim strMessage As String = String.Empty
    21.             Do
    22.  
    23.  
    24.                 Dim bytRecieved As Byte() =
    25.                udpClient2.Receive(iepRemoteEndPoint)
    26.                 strMessage = Encoding.ASCII.GetString(bytRecieved)
    27.  
    28.                 Console.WriteLine("This is the message you received: " _
    29.                + strMessage)
    30.  
    31.             Loop While (strMessage <> "exit")
    32.             udpClient2.Close()
    33.  
    34.  
    35.         Catch e As Exception
    36.  
    37.             Console.WriteLine(e.ToString())
    38.         End Try
    39.         Return 0
    40.     End Function
    Also, the size of the attachment suggests that it likely includes binary files in the archive, which is against forum rules. You MUST delete all bin and obj folders from your projects before zipping them for attachment.

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

    Re: udp send and receive

    I removed the attachment, as it did contain compiled code. Please delete the compiled code and re-attach.

    The issue may simply be that you are taking too long to read. UDP is VERY fast. I would suggest that you start listening before you send anything. Listen on a background thread, and when data is received, raise an event to the UI thread. I have a thread in the CodeBank that deals with broadcasting messages. You aren't doing that, but the receiver is on a different thread, so if you want an example of listening like that, it might be of some use.
    My usual boring signature: Nothing

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

    Re: udp send and receive

    I agree with Shaggy. There is no way that code would be able to receive the data sent because in terms of the speed at which these things operate, that data was sent long before it was ready to receive it. When dealing with network code in general, you want to be listening long before anything is actually sent.
    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

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