Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing clsUdpClient.vb (6.02 KB)
Option Explicit On
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.Encoding
Imports System.Threading
Public Class clsUdpClient
Implements IDisposable
Private thisSock As Socket = Nothing
Private thisThread As Thread = Nothing
Private hostEndpoint As EndPoint = Nothing
Private clientEndpoint As EndPoint = Nothing
Private callbackDataRecvStringFunc As UdpClientDataRecvStringFunc = Nothing
Private callbackDataRecvByteFunc As UdpClientDataRecvByteFunc = Nothing
Public Delegate Function UdpClientDataRecvStringFunc(ByVal strData As String) As String
Public Delegate Function UdpClientDataRecvByteFunc(ByVal byteData() As Byte) As Byte()
Private Const SOCKET_RECV_TIMEOUT = 2000 ' 2 seconds
Private Const SOCKET_SEND_TIMEOUT = 2000 ' 2 seconds
Private Const LOOP_SLEEP_DELAY = 10 ' 1/100 second
Public Sub New(ByVal ip As String, ByVal port As Integer)
' instantiate a new udp socket
thisSock = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
' configure the socket
thisSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, SOCKET_RECV_TIMEOUT)
thisSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, SOCKET_SEND_TIMEOUT)
hostEndpoint = New IPEndPoint(IPAddress.Parse(ip), port)
clientEndpoint = New IPEndPoint(IPAddress.Any, 0)
End Sub
Public ReadOnly Property Ready() As Boolean
Get
Return Not (thisSock Is Nothing)
End Get
End Property
Public ReadOnly Property IsListening() As Boolean
Get
Return Not (thisThread Is Nothing)
End Get
End Property
Public Overloads Sub StartListen(ByVal callbackFunc As UdpClientDataRecvStringFunc)
If thisThread Is Nothing Then
Me.callbackDataRecvStringFunc = callbackFunc
_startListen()
End If
End Sub
Public Overloads Sub StartListen(ByVal callbackFunc As UdpClientDataRecvByteFunc)
If thisThread Is Nothing Then
Me.callbackDataRecvByteFunc = callbackFunc
_startListen()
End If
End Sub
Public Sub StopListen()
If Not (thisThread Is Nothing) Then
thisThread.Abort()
thisThread = Nothing
End If
End Sub
Public Overloads Sub Send(ByVal strData As String)
Dim buff() As Byte = ASCII.GetBytes(strData)
Me.Send(buff)
End Sub
Public Overloads Sub Send(ByVal byteData() As Byte)
' send data to the host
thisSock.SendTo(byteData, 0, byteData.Length, _
SocketFlags.None, hostEndpoint)
End Sub
Public Overloads Function SendAndWait(ByVal strData As String) As String
Dim buff() As Byte = ASCII.GetBytes(strData)
Return ASCII.GetString(Me.SendAndWait(buff))
End Function
Public Overloads Function SendAndWait(ByVal byteData() As Byte) As Byte()
' send data to the host
thisSock.SendTo(byteData, 0, byteData.Length, _
SocketFlags.None, hostEndpoint)
Dim rBuff() As Byte = {}
Do ' infinite loop
If thisSock.Available > 0 Then
ReDim rBuff(thisSock.Available - 1)
' receive data from socket
thisSock.ReceiveFrom(rBuff, 0, _
thisSock.Available, SocketFlags.None, _
clientEndpoint)
End If
Loop Until rBuff.Length > 0
Return rBuff
End Function
Public Overrides Function ToString() As String
If Ready Then
Return thisSock.RemoteEndPoint.ToString
Else
Return MyBase.ToString()
End If
End Function
Public Sub Dispose() Implements IDisposable.Dispose
StopListen()
thisSock.Close()
End Sub
Private Sub _startListen()
thisThread = New Thread(AddressOf _threadedListen)
thisThread.Priority = ThreadPriority.BelowNormal
thisThread.IsBackground = True
thisThread.Start(Me)
End Sub
Private Sub _threadedListen(ByVal udpObject As Object)
Dim thisUdpClient As clsUdpClient = udpObject
Do ' infinite loop
If thisUdpClient.thisSock.Available > 0 Then
Dim rBuff() As Byte = {}
ReDim rBuff(thisUdpClient.thisSock.Available - 1)
Try
' receive data from socket
thisUdpClient.thisSock.ReceiveFrom(rBuff, thisUdpClient.clientEndpoint)
Catch
End Try
' invoke callback function
If Not (thisUdpClient.callbackDataRecvStringFunc Is Nothing) Then
Dim strData As String = thisUdpClient.callbackDataRecvStringFunc.Invoke( _
ASCII.GetString(rBuff))
If Not (strData Is Nothing) Then
Dim sBuff() As Byte = ASCII.GetBytes(strData)
' send data back to client
thisUdpClient.thisSock.SendTo(sBuff, 0, sBuff.Length, _
SocketFlags.None, thisUdpClient.hostEndpoint)
End If
ElseIf Not (thisUdpClient.callbackDataRecvByteFunc Is Nothing) Then
Dim byteData() As Byte = thisUdpClient.callbackDataRecvByteFunc.Invoke(rBuff)
If byteData.Length > 0 Then
' send data back to client
thisUdpClient.thisSock.SendTo(byteData, 0, byteData.Length, _
SocketFlags.None, thisUdpClient.hostEndpoint)
End If
End If
End If
' sleep for a duration of time
Thread.Sleep(LOOP_SLEEP_DELAY)
Loop
End Sub
End Class