Visual Studio .NET 2003Windows 2003.NET 1.1Windows 2000Windows XPIntermediateDevVisual StudioWindows.NETVisual Basic
UDP application for VB.NET






3.51/5 (31 votes)
Sep 3, 2003

133571

2512
The Sample help u basic understand how to develop UDP application under VB.NET
Introduction
AS my experience, the UDP protocol is convinent than TCP, especially your program is running Base LAN. it is enough reliable to comunicate and efficient. so I will provide one peer to peer comunicated application by UDP protocol.
Recent years, I have got a lot of benifits from codeproject Sites, So I also would like to
contribute my experince , share with u
I am from China, not be good at English.
The base class for UDP is presented as following:
#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
The Server Exampe: also has Client Function
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents cmdSend As System.Windows.Forms.Button
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.cmdSend = New System.Windows.Forms.Button
Me.TextBox2 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(32, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(256, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'cmdSend
'
Me.cmdSend.Location = New System.Drawing.Point(168, 64)
Me.cmdSend.Name = "cmdSend"
Me.cmdSend.Size = New System.Drawing.Size(128, 24)
Me.cmdSend.TabIndex = 1
Me.cmdSend.Text = "Send"
'
'TextBox2
'
Me.TextBox2.Location = New System.Drawing.Point(32, 160)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(264, 20)
Me.TextBox2.TabIndex = 2
Me.TextBox2.Text = "TextBox2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 317)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.cmdSend)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
xSock.UDP_Listen(10090)
AddHandler xSock.DataArrival, AddressOf OnDataRival
End Sub
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
xSock.UDP_Send("127.0.0.1", 10080, TextBox1.Text)
End Sub
Private Sub OnDataRival(ByVal vMsg As String)
TextBox2.Text = vMsg
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
xSock.CloseSock()
End Sub
End Class
Client Example, also has Server Function
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(24, 88)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(272, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(176, 160)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(120, 24)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Close Listen"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(176, 208)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 24)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Start Listen"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 309)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
xSock.UDP_Listen(10080)
AddHandler xSock.DataArrival, AddressOf UDPArrival
End Sub
Private Sub UDPArrival(ByVal vData As String)
TextBox1.Text = vData
xSock.UDP_Send("127.0.0.1", 10090, vData)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
xSock.CloseSock()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
xSock.CloseSock()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
xSock.UDP_Listen(10080)
End Sub
End Class
Thanks. if u have any comment that.