Click here to Skip to main content
15,879,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to broadcast a message to devices that reside on a wired network using a UDP broadcast as my devices will return a string identifier when they receive "SEEKING REPLY" string on port 30303

Everything works great until I turn my PC Wifi on - then nothing is found.

I've a suspicion that I'm either using something wrong in my endpoint addressing or perhaps not binding correctly so I've spent a couple of days researching this and trying different methods but I'm at the limits of my knowledge and not getting anywhere so I thought its time to ask for some help. Below is the code relating to this:

I'd appreciate any pointers to help me understand this, thanks.

What I have tried:

    Dim bytCommand As Byte() = New Byte() {}
    Dim receiveBytes As Byte() = New Byte() {}
    Dim strReturnData As String = String.Empty 'reply text
    Dim ListenPort As Integer = 12345

Dim udpBroadcast As New UdpClient(ListenPort) 'broadcast

    Dim DiscoveryIP As IPAddress = IPAddress.Parse("")
    Dim DiscoveryPort As Integer = 30303
    Dim DiscoveryEndpoint As New IPEndPoint(DiscoveryIP, DiscoveryPort)
    Dim DiscoveryMessage As String = "SEEKING REPLY" 'the message to broadcast

        'Problem is that with Wifi Active it doesnt pick up the ethernet
        'it only seems to broadcast to the first adapter (wifi)

    bytCommand = Encoding.UTF8.GetBytes(DiscoveryMessage  + Chr(13))

        udpBroadcast.EnableBroadcast = True

        udpBroadcast.Connect(DiscoveryEndpoint) 'Create a broadcast to UDP CLIENT

        udpBroadcast.Send(bytCommand, bytCommand.Length) 'send the DISCOVERY command
        udpBroadcast.Close() 'close the port

        Dim udpResponse As New UdpClient(ListenPort)

        udpResponse.Client.ReceiveTimeout = 10000 'set a 10 second timeout for searching '

        ReDim receiveBytes(1000) 'and space for the message,

        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, DiscoveryPort) 'Socket to listen in on

        'Start listening for reply
            While True 'There is data to harvest
                receiveBytes = udpResponse.Receive(RemoteIpEndPoint) 'capture replies, it will timeout if nothing to reply from
                strReturnData = strReturnData & Encoding.UTF8.GetString(receiveBytes).Trim & vbCrLf 'convert the byte array into a string
                udpResponse.Client.ReceiveTimeout = 1000 'reset a 1 second timeout for searching
            End While

            udpBroadcast = Nothing
            Return strReturnData '
        Catch ex As Exception 'it drops an exception when the timeout is reached

            udpBroadcast = Nothing

            If strReturnData = String.Empty Then MsgBox("Finished Searching but nothing found.")
            Return strReturnData 'return a NULL
        End Try

        udpBroadcast = Nothing
        Return strReturnData 'return a NULL

    Catch ex As Exception

        udpBroadcast = Nothing
        MsgBox("Error Searching " & ex.HResult & "," & ex.Message)
        Return strReturnData 'return a NULL
    End Try
MadMyche 11-Feb-19 10:12am    
What you are going to need to do is to enumerate through your list of network adapters and choose the one that you need. Take a look at this SO topic:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900