Communication Using UDP and Visual Basic


Application Security Testing: An Integral Part of DevOps

Isn't it amazing how the Internet has changed our way of communication? Imagine your life without email, or even Whatsapp. The Internet is, however, much larger than many people realize. Thanks to protocols, any communication on any platform is possible. Today, you will learn about UDP and see how to create an application to use it to communicate properly. Before jumping into the application, it is worth reviewing a few of the common communication protocols.

Common Protocols Used by VB Developers

A Protocol, or rather a Communication Protocol, is a set of rules that enables two or more entities to transmit information to one another. Some of the most common Protocols are:

  • TCP: TCP (Transmission Control Protocol) enables two systems to establish a connection and exchange data. TCP also guarantees delivery of data as well as that packets will be delivered in the same sequence in which they were sent.
  • IP: IP (Internet Protocol) specifies the format of packets (datagrams) and the addressing scheme.
  • UDP: UDP (User Datagram Protocol) is a connectionless protocol that is used for broadcasting messages over a network. UDP provides very, very few error recovery services.
  • POP: POP (Post Office Protocol) is a protocol used to retrieve e-mail from a mail server. Most e-mail clients make use the POP protocol, newer systems can use the newer IMAP (Internet Message Access Protocol).
  • SMTP: SMTP (Simple Mail Transfer Protocol) is a protocol for sending e-mail messages between servers.
  • HTTP: HTTP (HyperText Transfer Protocol) is the protocol used by the World Wide Web. HTTP also defines how messages should be formatted and transmitted, and what actions Web servers and browsers should take in response to the various commands.
  • FTP: FTP (File Transfer Protocol) is used protocol for exchanging files over the Internet.

Creating an UDP Program with Visual Basic

In this article, I'm going to show you how to create a Visual Basic UDP program. Actually, I'll show you how to make two communication programs! One app will serve as a sender of the UDP messages, and the other one will serve as the receiver of the sent UDP datagrams. This will be done by creating two Console Applications.

Following is an example of what our two programs will accomplish.

Figure 1: The Sender and Receiver applications running concurrently

The Visual Basic Sender Application

This application will be used to send pieces of information via UDP. Add the following Imports statements to your UDP Sender application:

Imports System.Net.Sockets
Imports System.Text

We will be working with the Sockets class inside the .NET namespace and do some text manipulation through the System.Text class. Add the following code:

   Sub Main()


      Dim UDPClient As New UdpClient()
      UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, _
         SocketOptionName.ReuseAddress, True)
      UDPClient.Connect("localhost", 11000)

         Dim strMessage As String = String.Empty

            strMessage = Console.ReadLine()
            Dim bytSent As Byte() = _

            UDPClient.Send(bytSent, bytSent.Length)

         Loop While strMessage <> String.Empty

      Catch e As Exception

      End Try

      Console.WriteLine("Press Any Key to Continue")

   End Sub

I created a new UdpClient object, connected to the localhost, and specified a port I would like to communicate over. It is very important that you remember the port number on your receiving client as well; otherwise, you will not be able to communicate through the port with it. Localhost is the local computer.

I then start formulating a message to be sent. In this case, the message that will be sent will be entered by the user. I encode the entered text as ASCII as we will be dealing with plain text with no special characters. As Enter is pressed in the command window, the message will be sent to the receiver that we will create now.

The Visual Basic Receiver Application

This application will be used to receive the messages through UDP. Add the following Imports to your application:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

The only addition here is the inclusion of the System.Net namespace. It is included in here because we will be creating IPEndPoints. Add the receiving code to your application:

   Sub Main()


      Dim UDPClient As UdpClient = New UdpClient()

      UDPClient.Client.SetSocketOption(SocketOptionLevel.Socket, _
         SocketOptionName.ReuseAddress, True)
      UDPClient.Client.Bind (New IPEndPoint(IPAddress.Any, 11000))


         Dim iepRemoteEndPoint As IPEndPoint = New _
            IPEndPoint(IPAddress.Any, 11000)
         Dim strMessage As String = String.Empty

            Dim bytRecieved As Byte() = _
            strMessage = Encoding.ASCII.GetString(bytRecieved)

            Console.WriteLine("This is the message you received: " _
               + strMessage)

         Loop While (strMessage <> "exit")

      Catch e As Exception

      End Try

      Console.WriteLine("Press Any Key to Continue")

   End Sub

I connect to the same port as the Sender's and then Bind it to an address. The receive method gets called to receive the message and displays it to the user.

You should be running both programs at once to see the communication in action.

I am attaching the samples here for you.


Using UDP for intercompany communication is quick and easy. Until next time, cheers!

About the Author

Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

Related Articles



  • Not work on two different computers

    Posted by Liang Lu on 02/16/2018 11:50am

    Hi, the sending/receiving works well when both programs are running at same machine but not on two different machines in the same network. Any ideas? Thanks, Liang

  • Web Developer

    Posted by Vinod on 07/03/2016 11:22pm

    Very well crafted article

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.