110,534
社区成员
发帖
与我相关
我的任务
分享
Public Class FrmMain
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New ChildForm
frm.ShowDialog(Me)
End Sub
End Class
Public Class ChildForm
Inherits System.Windows.Forms.Form
Private WithEvents Button1 As New Button
Private ready As Boolean = False
Private data() As Byte
Private Sub ChildForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler TestUdp.RData, AddressOf Test
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim b() As Byte = New Byte() {0, 10}
ready = False
Call TestUdp.Send(b)
Do
If ready Then
'处理数据 data()
End If
Loop '当然,还可以加判断超时的情况
End Sub
Private Sub Test(ByVal buffer() As Byte)
Data = buffer
ready = True
End Sub
End Class
Public Module Define
Public TestUdp As New Udp
End Module
Public Class Udp
Private UdpGetClient As New System.Net.Sockets.UdpClient(161)
Public Event RData(ByVal buffer() As Byte)
Public Sub New()
Dim th As New Threading.Thread(AddressOf ReceivedData)
th.Start()
End Sub
Public Sub Send(ByVal buffer() As Byte)
End Sub
Private Sub ReceivedData()
While True
Dim GetInIpEndPoint As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 161)
Try
Dim Item() As Byte = Me.UdpGetClient.Receive(GetInIpEndPoint)
RaiseEvent RData(Item)
Catch ex As Exception
End Try
End While
End Sub
End Class