Click here to Skip to main content
Click here to Skip to main content

UDP application for VB.NET

, 2 Sep 2003
Rate this:
Please Sign up or sign in to vote.
The Sample help u basic understand how to develop UDP application under VB.NET
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS--><!-- Download Links --> <!-- Add the rest of your HTML here -->

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:

<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 &#31383;&#20307;&#35774;&#35745;&#22120;&#29983;&#25104;&#30340;&#20195;&#30721; "<BR><BR>    Public Sub New()<BR>        MyBase.New()<BR><BR>        '&#35813;&#35843;&#29992;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;&#12290;<BR>        InitializeComponent()<BR><BR>        '&#22312; InitializeComponent() &#35843;&#29992;&#20043;&#21518;&#28155;&#21152;&#20219;&#20309;&#21021;&#22987;&#21270;<BR><BR>    End Sub<BR><BR>    '&#31383;&#20307;&#37325;&#20889; dispose &#20197;&#28165;&#29702;&#32452;&#20214;&#21015;&#34920;&#12290;<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 &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;<BR>    Private components As System.ComponentModel.IContainer<BR><BR>    '&#27880;&#24847;: &#20197;&#19979;&#36807;&#31243;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;<BR>    '&#21487;&#20197;&#20351;&#29992; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#20462;&#25913;&#27492;&#36807;&#31243;&#12290;<BR>    '&#19981;&#35201;&#20351;&#29992;&#20195;&#30721;&#32534;&#36753;&#22120;&#20462;&#25913;&#23427;&#12290;<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 &#31383;&#20307;&#35774;&#35745;&#22120;&#29983;&#25104;&#30340;&#20195;&#30721; "<BR><BR>    Public Sub New()<BR>        MyBase.New()<BR><BR>        '&#35813;&#35843;&#29992;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;&#12290;<BR>        InitializeComponent()<BR><BR>        '&#22312; InitializeComponent() &#35843;&#29992;&#20043;&#21518;&#28155;&#21152;&#20219;&#20309;&#21021;&#22987;&#21270;<BR><BR>    End Sub<BR><BR>    '&#31383;&#20307;&#37325;&#20889; dispose &#20197;&#28165;&#29702;&#32452;&#20214;&#21015;&#34920;&#12290;<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 &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;<BR>    Private components As System.ComponentModel.IContainer<BR><BR>    '&#27880;&#24847;: &#20197;&#19979;&#36807;&#31243;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;<BR>    '&#21487;&#20197;&#20351;&#29992; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#20462;&#25913;&#27492;&#36807;&#31243;&#12290;<BR>    '&#19981;&#35201;&#20351;&#29992;&#20195;&#30721;&#32534;&#36753;&#22120;&#20462;&#25913;&#23427;&#12290;<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

Share

About the Author

ahai163
Web Developer
China China
No Biography provided

Comments and Discussions

 
GeneralThese applications aren't worked in my computer Pinmemberjackiecch27-Apr-10 17:35 
GeneralUDP application for VB.NET Pinmemberimranfarooqui19-Jul-07 8:11 
GeneralNice work. PinmemberChathuranga Chandrasekara18-Dec-06 21:58 
Generalcommunication PinsussAnonymous19-May-05 5:07 
GeneralProblem sending data to client Pinmemberkhalidpal28-Apr-05 20:36 
GeneralClient/Server PinmemberVitoto12-Apr-05 11:47 
GeneralRe: Client/Server PinmemberMinddragon19-Oct-06 15:40 
GeneralSuggestions PinmemberD. Emilio Grimaldo Tuñon11-Apr-05 5:59 
GeneralNot bad PinmemberPhilggg7-Apr-04 15:05 
GeneralBinary Mode PinmemberVincent Pac Soo6-Nov-03 4:34 
GeneralRe: Binary Mode PinsussAnonymous14-Oct-04 6:43 
GeneralWhy everybody voted '1' Pinmemberdog_spawn3-Sep-03 7:19 
GeneralRe: Why everybody voted '1' Pinmembertrixcomp6-Nov-03 18:23 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 3 Sep 2003
Article Copyright 2003 by ahai163
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid