I’m broadcasting a simple message to ..*.255 (changing to 255 the last part of my ip) and i’m trying to listen to it. the code returns no error but i’m not receiving anything. In wireshark I can see the broacast is sent correctly, but with a different port each time (I don’t know if that’s a big deal). Here’s some parts of my code.
Private Sub connect() setip() btnsend.Enabled = True btndisconnect.Enabled = True btnconnect.Enabled = False receive() txtmsg.Enabled = True End Sub Sub receive() Try SocketNO = port rClient = New System.Net.Sockets.UdpClient(SocketNO) rClient.EnableBroadcast = True ThreadReceive = _ New System.Threading.Thread(AddressOf receivemessages) If ThreadReceive.IsAlive = False Then ThreadReceive.Start() Else ThreadReceive.Resume() End If Catch ex As Exception MsgBox('Error') End Try End Sub Sub receivemessages() Dim receiveBytes As Byte() = rClient.Receive(rip) Dim BitDet As BitArray BitDet = New BitArray(receiveBytes) Dim strReturnData As String = _ System.Text.Encoding.Unicode.GetString(receiveBytes) MsgBox(strReturnData.ToString) End Sub Private Sub setip() hostname = System.Net.Dns.GetHostName myip = IPAddress.Parse(System.Net.Dns.GetHostEntry(hostname).AddressList(1).ToString) ipsplit = myip.ToString.Split('.'.ToCharArray()) ipsplit(3) = 255 broadcastip = IPAddress.Parse(ipsplit(0) & '.' & ipsplit(1) & '.' + ipsplit(2) + '.' + ipsplit(3)) iep = New IPEndPoint(broadcastip, port) End Sub Sub sendmsg() Dim msg As Byte() MsgBox(myip.ToString) sclient = New UdpClient sclient.EnableBroadcast = True msg = Encoding.ASCII.GetBytes(txtmsg.Text) sclient.Send(msg, msg.Length, iep) sclient.Close() txtmsg.Clear() End Sub
This article seems to be doing almost exactly what you’re trying to do and explains it pretty well with lots of comments in the code.