Glossary Item Box
One advantage UDP has over TCP is the ability to send broadcast and multicast data. Broadcasting occurs when a host sends a datagram to all other hosts on the network. Multicasting occurs when a host sends a datagram to all hosts that are members of a particular multicast group (see definition below). Since TCP is connection-oriented, implying a sustained connection between two hosts, sending data to multiple hosts is impossible
Example 1: Broadcasting
Broadcasting is actually quite simple. Simply send a datagram to the specially designated broadcast address "255.255.255.255". All hosts listening on the wire will receive the datagram. A router will not forward a broadcast datagram, so it will be limited to the local network.
[C#] // Begin listening on the specified port udp1.Open(8888); // Send a broadcast to all hosts on the network on port 8888 udp1.Send("hello everyone", "255.255.255.255", 8888); // Receive the broadcast datagram. Datagram d = udp1.Receive(udp1.BufferSize); // Display data Debug.WriteLine(d.ToString()); /* Output * --------------------- * hello everyone * --------------------- */ [Visual Basic] ' Begin listening on the specified port Udp1.Open(8888) ' Send a broadcast to all hosts on the network on port 8888 Udp1.Send("hello everyone", "255.255.255.255", 8888) ' Receive the broadcast datagram. Dim d as Datagram = Udp1.Receive(Udp1.BufferSize) ' Display data Debug.WriteLine(d.ToString()) '* Output '* --------------------- '* hello everyone '* --------------------- '*
Example 2: Multicasting
Often broadcasting can provide unnecessary overhead. Take for example a network with 30 hosts. One host on this network is sending broadcast information which only 10 of the hosts of the network are interested in. This means that 20 hosts, who aren't even interested in the broadcast datagram, have to process the datagram up the protocol stack to the transport layer before it can determine it needs to discard the data. The 10 hosts would cause less strain on the non-participating hosts if they formed a multicast group.
To do this, the hosts simply need to join a multicast group. Multicast groups are specified as an IP Address within a specified range of addresses. This group of addresses, also know as class D IP Addresses, are in the range from 224.0.0.0 through 239.255.255.255. The 10 participating hosts simply need to join a multicast group in this range, then send datagrams to this multicast group address. The following example illustrates this. This code is dependent on the fact that you have a multi-line TextBox control called txtData, a TextBox control called txtSend, a Button control called cmdSend, and that you have implemented the EndReceive event. If you compiled the code below and ran the application on multiple hosts, you would have the basic framework for a simple chat program.
[C#] // The IPAddress representing the multicast address. private System.Net.IPAddress add = System.Net.IPAddress.Parse("239.255.255.254"); private void Form1_Load(object sender, System.EventArgs e) { // Allocate a socket for listening for datagrams. udp1.Open("MyHost", 1111); // Join the specified multicast group. udp1.JoinMulticastGroup(add); // Begin asynchronously receiving data byte[] buffer = new byte[udp1.BufferSize]; udp1.BeginReceive(buffer); } private void udp1_EndReceive(object sender, Dart.PowerTCP.SslSockets.DatagramEventArgs e) { // Data recived. Display txtData.Text += e.Datagram.ToString() + "\r\n"; // Begin asynchronously receiving data byte[] buffer = new byte[udp1.BufferSize]; udp1.BeginReceive(buffer); } private void cmdSend_Click(object sender, System.EventArgs e) { // Send button clicked. Send the contents of txtSend. udp1.Send(txtSend.Text, add.ToString(), 1111); } [Visual Basic] ' The IPAddress representing the multicast address. Private add As System.Net.IPAddress = System.Net.IPAddress.Parse("239.255.255.254") Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ' Allocate a socket for listening for datagrams. Udp1.Open("MyHost", 1111) ' Join the specified multicast group. Udp1.JoinMulticastGroup(add) ' Begin asynchronously receiving data Dim buffer(Udp1.BufferSize) As Byte Udp1.BeginReceive(buffer) End Sub Private Sub Udp1_EndReceive(ByVal sender As Object, ByVal e As Dart.PowerTCP.SslSockets.DatagramEventArgs) Handles Udp1.EndReceive ' Data recived. Display txtData.Text += e.Datagram.ToString() + vbCrLF ' Begin asynchronously receiving data Dim buffer(Udp1.BufferSize) As Byte Udp1.BeginReceive(buffer) End Sub Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click ' Send button clicked. Send the contents of txtSend. Udp1.Send(txtSend.Text, add.ToString(), 1111) End Sub
If you wished to leave the multicast group, simply add the following line of code.
[C#] udp1.LeaveMulticastGroup(add); [Visual Basic] Udp1.LeaveMulticastGroup(add)
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.