C# .NET Web Developer's Guide

In this section, you rewrite the example from the section Example TCP Command Transmission and Processing for using UDP as the transport protocol. Refer back to that section for the introduction to the architecture and the implemented communication protocol.
| Note | As mentioned earlier, UDP is normally not used for request/response protocols like client/server command processing. This example is used for showing the differences in using UDP and TCP. |
In contrast to TCP, in using UDP only one main network class is needed for this example. This is because the handling is like peer-to-peer (P2P). On both sides (client and server), we use System.Net.Sockets.UdpClient.
As a matter of principle, we can say a UDP client binds to a local port from which it receives data. Data is sent directly to another UDP client without connecting explicitly. That is what is meant by connection-less communication.
Generally, the code on both sides looks the same. A UdpClient is bound to a local port. Now it is ready to send and receive data. Because you bind the client to a local port only, you must use one Send() method that needs the remote host connection information. This information is used for sending the data to another UDP client. Because you bind the UdpClient to a local port, you receive data from this port, and you do not have to specify a receive point for the Receive() method. That is the...