Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Dim ip As String = txt_Dst_IP.Text '""
Dim port As Integer = CType(txt_Port.Text, Integer)
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim udpIP As IPAddress = IPAddress.Parse(ip)
Dim udp_endpoint As New IPEndPoint(udpIP, port)
Dim groupEP As New IPEndPoint(IPAddress.Any, port)
Dim senderRemote As EndPoint = CType(groupEP, EndPoint)
Dim WistronId As String = ""
Try
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, True) ‘设定允许socket广播
s.Bind(groupEP) ‘要收数据包,必须绑定
s.SendTimeout = 1000 '设定发送超时为1s
s.SendTo(s_buf, udp_endpoint) '发送, s_buf是字节数组名
Dim msg() As Byte = New [Byte](255) {} '定义一个255字节的接收缓冲区
s.ReceiveTimeout = 200 '设定接收超时为200ms
Dim i As Integer = 0
While i < 16
i += 1
s.ReceiveFrom(msg, senderRemote)
Dim str As String = ""
str = Encoding.ASCII.GetString(msg) 'CStr(msg(i))
groupEP = CType(senderRemote, IPEndPoint) '为了获取接收到的数据包源IP
If msg(25) = &H99 And msg(26) = &H2B Then
For n As Integer = 24 To 26 Step 1
WistronId &= " " + Hex(msg(n))
Next
WistronId = "Wistron ID: " + WistronId
Else
WistronId = " Not-RCB-Device"
End If
Lstbox_receive.Items.Add(i.ToString & " " & groupEP.Address.ToString & " " & WistronId)
WistronId = ""
End While
Catch ex As Exception
MessageBox.Show(ex.ToString, "提示")
Finally
s.Close()
End Try