听UDP广播与线程 [英] Listening to a UDP broadcast with threads
本文介绍了听UDP广播与线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我广播一个简单的消息的 的。*。255(切换到255我的IP的最后一部分),我想听听它。在code返回任何错误,但我没有收到任何东西。在Wireshark中我可以看到broacast被正确发送,但每一次不同的端口(我不知道这是一个大问题)。下面是我的code某些部分。
私人小组连接()
SETIP()
btnsend.Enabled = TRUE
btndisconnect.Enabled = TRUE
btnconnect.Enabled =假
接收()
txtmsg.Enabled = TRUE
结束小组
子接收()
尝试
SocketNO =口
rClient =新System.Net.Sockets.UdpClient(SocketNO)
rClient.EnableBroadcast = TRUE
ThreadReceive = _
新System.Threading.Thread(AddressOf receivemessages)
如果ThreadReceive.IsAlive = false,那么
ThreadReceive.Start()
其他
ThreadReceive.Resume()
结束如果
抓住EX为例外
MSGBOX(错误)
结束尝试
结束小组
子receivemessages()
昏暗receiveBytes为字节()= rClient.Receive(RIP)
昏暗BitDet作为BitArray
BitDet =新BitArray(receiveBytes)
昏暗strReturnData作为字符串= _
System.Text.Encoding.Uni code.GetString(receiveBytes)
MSGBOX(strReturnData.ToString)
结束小组
私人小组SETIP()
主机名= System.Net.Dns.GetHostName
MYIP = IPAddress.Parse(System.Net.Dns.GetHostEntry(主机名).AddressList(1)的ToString)
ipsplit = myip.ToString.Split(。。ToCharArray())
ipsplit(3)= 255
broadcastip = IPAddress.Parse(ipsplit(0)&安培;&安培。; ipsplit(1)及。。+ ipsplit(2)+ ... + ipsplit(3))
IEP =新IPEndPoint(broadcastip,口)
结束小组
子SENDMSG()
昏暗的味精为字节()
MSGBOX(myip.ToString)
sclient =新UdpClient
sclient.EnableBroadcast = TRUE
味精= Encoding.ASCII.GetBytes(txtmsg.Text)
sclient.Send(味精,msg.Length,IEP)
sclient.Close()
txtmsg.Clear()
结束小组
解决方案
本文似乎是在做几乎正是你想要做什么,并且解释了pretty的很好,有很多在code意见。
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.
这篇关于听UDP广播与线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文