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("255.255.255.255") Dim DiscoveryPort As Integer = 30303 Dim DiscoveryEndpoint As New IPEndPoint(DiscoveryIP, DiscoveryPort) Dim DiscoveryMessage As String = "SEEKING REPLY" 'the message to broadcast Try '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 Try 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.Close() udpResponse.Close() udpBroadcast = Nothing Return strReturnData ' Catch ex As Exception 'it drops an exception when the timeout is reached udpBroadcast.Close() udpResponse.Close() udpBroadcast = Nothing If strReturnData = String.Empty Then MsgBox("Finished Searching but nothing found.") Return strReturnData 'return a NULL End Try udpBroadcast.Close() udpResponse.Close() udpBroadcast = Nothing Return strReturnData 'return a NULL Catch ex As Exception udpBroadcast.Close() udpBroadcast = Nothing MsgBox("Error Searching " & ex.HResult & "," & ex.Message) Return strReturnData 'return a NULL End Try
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)