Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / Visual Basic
Article

UDP application for VB.NET

Rate me:
Please Sign up or sign in to vote.
3.51/5 (31 votes)
2 Sep 2003 131.5K   2.5K   24   13
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:

VB.NET
<BR><BR>#Region "info - English"<BR>''' The Example will help u a fully understand UDP, Thread, and Encoding<BR>''' Through the Example, U will see how to solve the Blocking problem about <BR>''' Receive function related UDP.<BR>''' U can try it.<BR>#End Region <BR>#Region "Imports"<BR>Imports System<BR>Imports System.IO<BR>Imports System.Net<BR>Imports System.Net.Sockets<BR>Imports System.Text<BR>Imports System.Threading<BR>#End Region <BR><BR>Public Class xSock <BR>#Region "Declares"<BR>    Private Shared UDP_Client As New UdpClient<BR>    Private Shared UDP_Server_Port As Integer<BR>    Private Shared thdUdp As Thread<BR>    Private Shared UDP_Server As UdpClient<BR>#End Region <BR>#Region "Events"<BR>    'Public Event Close()<BR>    Public Shared Event DataArrival(ByVal Data As String)<BR>    Public Shared Event Sock_Error(ByVal Description As String)<BR>#End Region <BR><BR>#Region "UDP" <BR>    Public Shared Sub UDP_Send(ByVal Host As String, ByVal Port As Integer,_<BR>  ByVal Data As String)<BR>        Try<BR>            UDP_Client.Connect(Host, Port)<BR>            Dim sendBytes As [Byte]() = Encoding.Unicode.GetBytes(Data)<BR>            UDP_Client.Send(sendBytes, sendBytes.Length) <BR>        Catch e As Exception<BR>            RaiseEvent Sock_Error(e.ToString)<BR>        End Try <BR>    End Sub <BR>    Public Shared Function UDP_Listen(ByVal Port As Integer) As Boolean<BR>        Try<BR>            UDP_Server_Port = Port<BR>            UDP_Server = New UdpClient(Port)<BR>            thdUdp = New Thread(AddressOf GetUDPData)<BR>            thdUdp.Start()<BR>        Catch e As Exception<BR>            RaiseEvent Sock_Error(e.ToString)<BR>        End Try <BR>    End Function <BR>    Private Shared Sub GetUDPData()<BR>        Do While True<BR>            Try<BR>                Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)<BR>                Dim RData = Encoding.Unicode.GetString(_<BR>UDP_Server.Receive(RemoteIpEndPoint))<BR>                RaiseEvent DataArrival(RData)<BR>                If RData = "CloseMe" Then Exit Do<BR>                Thread.Sleep(0)<BR>            Catch e As Exception<BR>                RaiseEvent Sock_Error(e.ToString)<BR>            End Try<BR>        Loop<BR>    End Sub <BR>    Public Shared Sub CloseSock()<BR>        UDP_Send("127.0.0.1", UDP_Server_Port, "CloseMe")<BR>        Thread.Sleep(30)<BR>        UDP_Server.Close()<BR>        thdUdp.Abort()<BR>    End Sub<BR>#End Region <BR>End Class<BR><BR> The Server Exampe: also has Client Function<BR><BR>Public Class Form1<BR>    Inherits System.Windows.Forms.Form<BR><BR>#Region " Windows 窗体设计器生成的代码 "<BR><BR>    Public Sub New()<BR>        MyBase.New()<BR><BR>        '该调用是 Windows 窗体设计器所必需的。<BR>        InitializeComponent()<BR><BR>        '在 InitializeComponent() 调用之后添加任何初始化<BR><BR>    End Sub<BR><BR>    '窗体重写 dispose 以清理组件列表。<BR>    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)<BR>        If disposing Then<BR>            If Not (components Is Nothing) Then<BR>                components.Dispose()<BR>            End If<BR>        End If<BR>        MyBase.Dispose(disposing)<BR>    End Sub<BR><BR>    'Windows 窗体设计器所必需的<BR>    Private components As System.ComponentModel.IContainer<BR><BR>    '注意: 以下过程是 Windows 窗体设计器所必需的<BR>    '可以使用 Windows 窗体设计器修改此过程。<BR>    '不要使用代码编辑器修改它。<BR>    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox<BR>    Friend WithEvents cmdSend As System.Windows.Forms.Button<BR>    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox<BR>    <System.Diagnostics.DebuggerStepThrough()> _<BR>    Private Sub InitializeComponent()<BR>        Me.TextBox1 = New System.Windows.Forms.TextBox<BR>        Me.cmdSend = New System.Windows.Forms.Button<BR>        Me.TextBox2 = New System.Windows.Forms.TextBox<BR>        Me.SuspendLayout()<BR>        '<BR>        'TextBox1<BR>        '<BR>        Me.TextBox1.Location = New System.Drawing.Point(32, 32)<BR>        Me.TextBox1.Name = "TextBox1"<BR>        Me.TextBox1.Size = New System.Drawing.Size(256, 20)<BR>        Me.TextBox1.TabIndex = 0<BR>        Me.TextBox1.Text = "TextBox1"<BR>        '<BR>        'cmdSend<BR>        '<BR>        Me.cmdSend.Location = New System.Drawing.Point(168, 64)<BR>        Me.cmdSend.Name = "cmdSend"<BR>        Me.cmdSend.Size = New System.Drawing.Size(128, 24)<BR>        Me.cmdSend.TabIndex = 1<BR>        Me.cmdSend.Text = "Send"<BR>        '<BR>        'TextBox2<BR>        '<BR>        Me.TextBox2.Location = New System.Drawing.Point(32, 160)<BR>        Me.TextBox2.Name = "TextBox2"<BR>        Me.TextBox2.Size = New System.Drawing.Size(264, 20)<BR>        Me.TextBox2.TabIndex = 2<BR>        Me.TextBox2.Text = "TextBox2"<BR>        '<BR>        'Form1<BR>        '<BR>        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)<BR>        Me.ClientSize = New System.Drawing.Size(328, 317)<BR>        Me.Controls.Add(Me.TextBox2)<BR>        Me.Controls.Add(Me.cmdSend)<BR>        Me.Controls.Add(Me.TextBox1)<BR>        Me.Name = "Form1"<BR>        Me.Text = "Form1"<BR>        Me.ResumeLayout(False)<BR><BR>    End Sub<BR><BR>#End Region<BR><BR>    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR>        xSock.UDP_Listen(10090)<BR>        AddHandler xSock.DataArrival, AddressOf OnDataRival<BR>    End Sub<BR><BR>    Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click<BR>        xSock.UDP_Send("127.0.0.1", 10080, TextBox1.Text)<BR>    End Sub<BR><BR>    Private Sub OnDataRival(ByVal vMsg As String)<BR>        TextBox2.Text = vMsg<BR>    End Sub<BR>    Private Sub Form1_Closing(ByVal sender As Object, _<BR> ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing<BR>        xSock.CloseSock()<BR>    End Sub<BR>End Class<BR><BR><BR>Client Example, also has Server Function<BR><BR>Imports System.Threading<BR>Public Class Form1<BR>    Inherits System.Windows.Forms.Form<BR><BR>#Region " Windows 窗体设计器生成的代码 "<BR><BR>    Public Sub New()<BR>        MyBase.New()<BR><BR>        '该调用是 Windows 窗体设计器所必需的。<BR>        InitializeComponent()<BR><BR>        '在 InitializeComponent() 调用之后添加任何初始化<BR><BR>    End Sub<BR><BR>    '窗体重写 dispose 以清理组件列表。<BR>    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)<BR>        If disposing Then<BR>            If Not (components Is Nothing) Then<BR>                components.Dispose()<BR>            End If<BR>        End If<BR>        MyBase.Dispose(disposing)<BR>    End Sub<BR><BR>    'Windows 窗体设计器所必需的<BR>    Private components As System.ComponentModel.IContainer<BR><BR>    '注意: 以下过程是 Windows 窗体设计器所必需的<BR>    '可以使用 Windows 窗体设计器修改此过程。<BR>    '不要使用代码编辑器修改它。<BR>    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox<BR>    Friend WithEvents Button1 As System.Windows.Forms.Button<BR>    Friend WithEvents Button2 As System.Windows.Forms.Button<BR>    <System.Diagnostics.DebuggerStepThrough()> _<BR>      Private Sub InitializeComponent()<BR>        Me.TextBox1 = New System.Windows.Forms.TextBox<BR>        Me.Button1 = New System.Windows.Forms.Button<BR>        Me.Button2 = New System.Windows.Forms.Button<BR>        Me.SuspendLayout()<BR>        '<BR>        'TextBox1<BR>        '<BR>        Me.TextBox1.Location = New System.Drawing.Point(24, 88)<BR>        Me.TextBox1.Name = "TextBox1"<BR>        Me.TextBox1.Size = New System.Drawing.Size(272, 20)<BR>        Me.TextBox1.TabIndex = 0<BR>        Me.TextBox1.Text = "TextBox1"<BR>        '<BR>        'Button1<BR>        '<BR>        Me.Button1.Location = New System.Drawing.Point(176, 160)<BR>        Me.Button1.Name = "Button1"<BR>        Me.Button1.Size = New System.Drawing.Size(120, 24)<BR>        Me.Button1.TabIndex = 1<BR>        Me.Button1.Text = "Close Listen"<BR>        '<BR>        'Button2<BR>        '<BR>        Me.Button2.Location = New System.Drawing.Point(176, 208)<BR>        Me.Button2.Name = "Button2"<BR>        Me.Button2.Size = New System.Drawing.Size(120, 24)<BR>        Me.Button2.TabIndex = 2<BR>        Me.Button2.Text = "Start Listen"<BR>        '<BR>        'Form1<BR>        '<BR>        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)<BR>        Me.ClientSize = New System.Drawing.Size(328, 309)<BR>        Me.Controls.Add(Me.Button2)<BR>        Me.Controls.Add(Me.Button1)<BR>        Me.Controls.Add(Me.TextBox1)<BR>        Me.Name = "Form1"<BR>        Me.Text = "Form1"<BR>        Me.ResumeLayout(False)<BR><BR>    End Sub<BR><BR>#End Region<BR><BR> <BR><BR><BR>    Private Sub Form1_Load(ByVal sender As System.Object, _<BR>            ByVal e As System.EventArgs) Handles MyBase.Load<BR>        xSock.UDP_Listen(10080)<BR>        AddHandler xSock.DataArrival, AddressOf UDPArrival<BR>    End Sub<BR>    Private Sub UDPArrival(ByVal vData As String)<BR>        TextBox1.Text = vData<BR>        xSock.UDP_Send("127.0.0.1", 10090, vData)<BR>    End Sub<BR><BR>    Private Sub Form1_Closing(ByVal sender As Object, _<BR>  ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing<BR>        xSock.CloseSock()<BR>    End Sub<BR><BR>    Private Sub Button1_Click(ByVal sender As System.Object, _<BR>           ByVal e As System.EventArgs) Handles Button1.Click<BR>        xSock.CloseSock()<BR>    End Sub<BR><BR>    Private Sub Button2_Click(ByVal sender As System.Object, _<BR>         ByVal e As System.EventArgs) Handles Button2.Click<BR>        xSock.UDP_Listen(10080)<BR>    End Sub<BR>End Class<BR><BR>

 

Thanks. if u have any comment that.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThese applications aren't worked in my computer Pin
jackiecch27-Apr-10 16:35
jackiecch27-Apr-10 16:35 
GeneralUDP application for VB.NET Pin
imranfarooqui19-Jul-07 7:11
imranfarooqui19-Jul-07 7:11 
I only needed some hint, you gave me more than that. THanks buddy!
GeneralNice work. Pin
Chathuranga Chandrasekara18-Dec-06 20:58
Chathuranga Chandrasekara18-Dec-06 20:58 
Generalcommunication Pin
Anonymous19-May-05 4:07
Anonymous19-May-05 4:07 
GeneralProblem sending data to client Pin
khalidpal28-Apr-05 19:36
khalidpal28-Apr-05 19:36 
GeneralClient/Server Pin
Vitoto12-Apr-05 10:47
Vitoto12-Apr-05 10:47 
GeneralRe: Client/Server Pin
Minddragon19-Oct-06 14:40
Minddragon19-Oct-06 14:40 
GeneralSuggestions Pin
Lord of Scripts11-Apr-05 4:59
Lord of Scripts11-Apr-05 4:59 
GeneralNot bad Pin
Philggg7-Apr-04 14:05
Philggg7-Apr-04 14:05 
GeneralBinary Mode Pin
Vincent Pac Soo6-Nov-03 3:34
Vincent Pac Soo6-Nov-03 3:34 
GeneralRe: Binary Mode Pin
Anonymous14-Oct-04 5:43
Anonymous14-Oct-04 5:43 
GeneralWhy everybody voted '1' Pin
dog_spawn3-Sep-03 6:19
dog_spawn3-Sep-03 6:19 
GeneralRe: Why everybody voted '1' Pin
trixcomp6-Nov-03 17:23
trixcomp6-Nov-03 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.