G
Guest
Hello,
i have a big problem. I want to code a UDP Server for my pocketPC with
VB.NET. I found some Examples in the net... but only for "normal" .NET
coding. So no examples works with the Compact Framework. I have one really
good example, ... can you tell me what i have to change for working with my
Project?
Sorry for my bad english ...
Martin
Code:
Example Code:
#Region "info - English"
''' The Example will help u a fully understand UDP, Thread, and Encoding
''' Through the Example, U will see how to solve the Blocking problem about
Receive function related UDP.
''' U can try it.
#End Region
#Region "Imports"
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
#End Region
Public Class xSock
#Region "Declares"
Private Shared UDP_Client As New UdpClient
Private Shared UDP_Server_Port As Integer
Private Shared thdUdp As Thread
Private Shared UDP_Server As UdpClient
#End Region
#Region "Events"
'Public Event Close()
Public Shared Event DataArrival(ByVal Data As String)
Public Shared Event Sock_Error(ByVal Description As String)
#End Region
#Region "UDP"
Public Shared Sub UDP_Send(ByVal Host As String, ByVal Port As Integer,
ByVal Data As String)
Try
UDP_Client.Connect(Host, Port)
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(Data)
UDP_Client.Send(sendBytes, sendBytes.Length)
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
End Sub
Public Shared Function UDP_Listen(ByVal Port As Integer) As Boolean
Try
UDP_Server_Port = Port
UDP_Server = New UdpClient(Port)
thdUdp = New Thread(AddressOf GetUDPData)
thdUdp.Start()
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
End Function
Private Shared Sub GetUDPData()
Do While True
Try
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim RData =
Encoding.Unicode.GetString(UDP_Server.Receive(RemoteIpEndPoint))
RaiseEvent DataArrival(RData)
If RData = "CloseMe" Then Exit Do
Thread.Sleep(0)
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
Loop
End Sub
Public Shared Sub CloseSock()
UDP_Send("127.0.0.1", UDP_Server_Port, "CloseMe")
Thread.Sleep(30)
UDP_Server.Close()
thdUdp.Abort()
End Sub
#End Region
End Class
i have a big problem. I want to code a UDP Server for my pocketPC with
VB.NET. I found some Examples in the net... but only for "normal" .NET
coding. So no examples works with the Compact Framework. I have one really
good example, ... can you tell me what i have to change for working with my
Project?
Sorry for my bad english ...
Martin
Code:
Example Code:
#Region "info - English"
''' The Example will help u a fully understand UDP, Thread, and Encoding
''' Through the Example, U will see how to solve the Blocking problem about
Receive function related UDP.
''' U can try it.
#End Region
#Region "Imports"
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
#End Region
Public Class xSock
#Region "Declares"
Private Shared UDP_Client As New UdpClient
Private Shared UDP_Server_Port As Integer
Private Shared thdUdp As Thread
Private Shared UDP_Server As UdpClient
#End Region
#Region "Events"
'Public Event Close()
Public Shared Event DataArrival(ByVal Data As String)
Public Shared Event Sock_Error(ByVal Description As String)
#End Region
#Region "UDP"
Public Shared Sub UDP_Send(ByVal Host As String, ByVal Port As Integer,
ByVal Data As String)
Try
UDP_Client.Connect(Host, Port)
Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(Data)
UDP_Client.Send(sendBytes, sendBytes.Length)
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
End Sub
Public Shared Function UDP_Listen(ByVal Port As Integer) As Boolean
Try
UDP_Server_Port = Port
UDP_Server = New UdpClient(Port)
thdUdp = New Thread(AddressOf GetUDPData)
thdUdp.Start()
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
End Function
Private Shared Sub GetUDPData()
Do While True
Try
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
Dim RData =
Encoding.Unicode.GetString(UDP_Server.Receive(RemoteIpEndPoint))
RaiseEvent DataArrival(RData)
If RData = "CloseMe" Then Exit Do
Thread.Sleep(0)
Catch e As Exception
RaiseEvent Sock_Error(e.ToString)
End Try
Loop
End Sub
Public Shared Sub CloseSock()
UDP_Send("127.0.0.1", UDP_Server_Port, "CloseMe")
Thread.Sleep(30)
UDP_Server.Close()
thdUdp.Abort()
End Sub
#End Region
End Class