PS: Вот код
- Код: Выделить всё
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Imports System.Threading
Imports System.ComponentModel
Public Class UDPMobile
Inherits Component
Dim NewThread As New ThreadStart(AddressOf Start)
Dim ThreadStart As New Thread(NewThread)
Dim port As Integer
Public Shared Event DataArrival(ByVal data As String) 'Событие получения данных
Private Sub Start()
StartListen()
End Sub
Public Sub StartListener(ByVal NumberPort As Integer)
port = NumberPort
ThreadStart.Start()
End Sub
Private Sub StartListen()
Dim done As Boolean = False
Dim listener As New UdpClient(port)
Dim groupEP As New IPEndPoint(IPAddress.Any, port)
Try
While Not done
Dim bytes As Byte() = listener.Receive(groupEP)
RaiseEvent DataArrival(Encoding.Default.GetString(bytes, 0, bytes.Length))
End While
Catch e As Exception
MsgBox(e.ToString())
Finally
listener.Close()
End Try
End Sub
End Class