Find all our projects in development below.
All source code is GNU General Public License (GPL)
Browsing clsUdpServer.vb (5.03 KB)
Option Explicit On
Imports System.Net
Imports System.Net.Sockets
Imports System.Text.Encoding
Imports System.Threading
Public Class clsUdpServer
Implements IDisposable
Private thisSock As Socket = Nothing
Private thisThread As Thread = Nothing
Private clientEndpoint As EndPoint = Nothing
Private callbackDataRecvStringFunc As UdpServerDataRecvStringFunc = Nothing
Private callbackDataRecvByteFunc As UdpServerDataRecvByteFunc = Nothing
Public Delegate Function UdpServerDataRecvStringFunc(ByVal strData As String) As String
Public Delegate Function UdpServerDataRecvByteFunc(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)
Dim hostEndpoint As IPEndPoint = Nothing
If ip Is Nothing Then
hostEndpoint = New IPEndPoint(IPAddress.Any, port)
Else
hostEndpoint = New IPEndPoint(IPAddress.Parse(ip), port)
End If
thisSock.Bind(hostEndpoint)
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 UdpServerDataRecvStringFunc)
If thisThread Is Nothing Then
Me.callbackDataRecvStringFunc = callbackFunc
_startListen()
End If
End Sub
Public Overloads Sub StartListen(ByVal callbackFunc As UdpServerDataRecvByteFunc)
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 back to client
thisSock.SendTo(byteData, 0, byteData.Length, _
SocketFlags.None, clientEndpoint)
End Sub
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 thisUdpServer As clsUdpServer = udpObject
Do ' infinite loop
If thisUdpServer.thisSock.Available > 0 Then
Dim rBuff() As Byte = {}
ReDim rBuff(thisUdpServer.thisSock.Available - 1)
' receive data from socket
thisUdpServer.thisSock.ReceiveFrom(rBuff, thisUdpServer.clientEndpoint)
' invoke callback function
If Not (thisUdpServer.callbackDataRecvStringFunc Is Nothing) Then
Dim strData As String = thisUdpServer.callbackDataRecvStringFunc.Invoke( _
ASCII.GetString(rBuff))
If Not (strData Is Nothing) Then
Dim sBuff() As Byte = ASCII.GetBytes(strData)
' send data back to client
thisUdpServer.thisSock.SendTo(sBuff, 0, sBuff.Length, _
SocketFlags.None, thisUdpServer.clientEndpoint)
End If
ElseIf Not (thisUdpServer.callbackDataRecvByteFunc Is Nothing) Then
Dim byteData() As Byte = thisUdpServer.callbackDataRecvByteFunc.Invoke(rBuff)
If byteData.Length > 0 Then
' send data back to client
thisUdpServer.thisSock.SendTo(byteData, 0, byteData.Length, _
SocketFlags.None, thisUdpServer.clientEndpoint)
End If
End If
End If
' sleep for a duration of time
Thread.Sleep(LOOP_SLEEP_DELAY)
Loop
End Sub
End Class