UDP Communication

From Xojo Documentation

The User Datagram Protocol, or UDP, is the basis for most high speed, highly distributed network traffic. It is a connectionless protocol that has very low overhead, but is not as secure or reliable as TCP. The UDPSocket provides access to this protocol.

Like the TCPSocket control, the UDPSocket control is derived from the SocketCore class.

In order to use a UDPSocket, it must be bound to a specific port on your machine. Once bound, the UDPSocket is ready for use. It immediately begins accepting any data that it sees on the port that it has bound to. It also allows you to send data out, as well as set UDP socket options.


In order to differentiate between which machine is sending you what data, a UDPSocket uses a data structure known as a Datagram. A Datagram is a built-in class with two properties, Address, which is the IP address of the remote machine that sent you the data, and Data — the actual data itself. When you attempt to send data out, you must specify information in the form of a Datagram. This information is the remote address of the machine you want to receive your packet, the port it should be sent to, and the data you wish to send to the remote machine.

UDPSocket Modes

UDP sockets can operate in various modes which are all very similar, but have vastly different uses. The mode that most resembles a TCP communication is called “unicasting.” This occurs when the IP address you specify when you write data out is that of a single machine. An example would be sending data to, or some other network address. It is a Datagram that has one intended receiver.

This code sends data to a specific machine:

Var data As New Datagram
data.Address = ""
data.Data = "Hello, World!"
data.Port = 9500

The second mode of operation is called “broadcasting”. As the name implies, this is a broadcasted write. It is akin to yelling into a megaphone. Everyone gets the message, whether they want to or not. If the machine happens to be listening on the port you specified, then it will receive the data on that port. This type of send is very network intensive. As you can imagine, broadcasting can create a huge amount of network traffic. The good news is, when you broadcast data out, it does not leave your subnet. Basically, a broadcast send will not leave your local network to travel out into the world. When you want to broadcast data, instead of sending the data to an IP address of a remote machine, you specify the broadcast address for your machine. This address changes from machine to machine, so there is a property of the UDPSocket class that tells you the correct broadcast address.

This code broadcasts a message to all machines on your local network:

Var data As String
data = "Hello, World!"

// broadcasting the message
UDPSocket1.Write(UDPSocket1.BroadcastAddress, data)

The third mode of operation for UDPSockets is “multicasting”. It is a combination of unicasting and broadcasting that is very powerful and practical. Multicasting is a lot like a chat room: you enter the chatroom, and are able to hold conversations with everyone else in the chatroom. When you want to enter the chatroom, you call JoinMulticastGroup, and you only need to specify the group you wish to join. The group parameter is a special kind of IP address, called a Class D IP. They range from to

Think of the IP address as the name of the chatroom. If you want to start chatting with two other people, all three of you need to call JoinMulticastGroup with the same Class D IP address specified as the group. When you wish to leave the chatroom, you only need to call LeaveMulticastGroup, and again, specify the group you wish to leave. You can join as many multicast groups as you like, you are not limited to just one at a time. When you wish to send data to the multicast group, you only need to specify the multicast group’s IP address. All people who have joined the same group as you will receive the message.

NetTank game using UDP for communication

This code sends data to members of the multicast group:

Var data As String

data = "Hello, World!"
If UDPSocket1.JoinMulticastGroup("") Then

 UDPSocket1.Write("", data)
End If

Example Projects

  • Examples/Communication/Internet/UDPExample
  • NetTank (A tank shooting game that you can play over the network using UDP)

See Also

UDPSocket, Datagram classes