UDP with VB.NET

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
 
G

Guest

Hi Alex,
first of all thanks for your reply...

I got this example from codeproject.com its normally for Windows Desktop
Developing. But it doesnt work at my VB.NET (for Pocket PC 2003) Project....i
get this errors:

cxSock.vb(66): For the parameter "count" from "Public Overridable Function
GetString(bytes() As Byte, index As Integer, count As Integer) As String"
where no argument declared.

cxSock.vb(66): For the parameter "count" from "Public Overridable Function
GetString(bytes() As Byte, index as Integer, count as Integer) As String"
where no argument declared.

cxSock.vb(66): For the parameter "index" from "Public Overridable Function
GetString(bytes() as Byte, index as Integer, count as Integer) As String"
where no argument declared.

cxSock.vb(80): "abort" is no Member from "System.Threading.Thread".

Thanks for help

Martin

Alex Yakhnin said:
Generally, the samples should work in .NetCF.

--
Alex Yakhnin, .NET CF MVP
www.intelliprog.com
www.opennetcf.org

MartinFH said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top