One way to understand how UDP works is by comparing it to TCP.
TCP
-
Connection : In order to use the TCP protocol, you need to establish a connection to a remote (and only) host.
-
Two-way communication : Once the connection is established, the two points can send messages (which is why IP tunneling works)
-
Peer : A connection involves only two points.
-
Guaranteed Transmission : Once a packet has been sent to a remote host, the protocol monitors the delivery (or warns you of faults).
UDP
- Noconnection:YoudonotestablishconnectionswhenissuingUDPpackets;onlyspecifiesatarget.
- Nowarranties:Theprotocoldoesnotimplementanykindofguaranteethatthepacketwillreachthedestinationhost.
- Usingmasks:Apacketcanbesenttomultiplehostsatthesametimewhenthetargetisamask.
Intheexamplebelow,aUDPpacketsenttotheaddress192.168.1.255
fromhost192.168.1.15willbereceivedbyallhostsofthesubnet,including192.168.1.15;Theoctet.255
isequivalent,ingeneraltermsandinthegivenexample,to'allhostsunder192.168.1'.
UDP packet issue example (in C #):
static void SendUdp(int srcPort, string dstIp, int dstPort, byte[] data)
{
using (UdpClient c = new UdpClient(srcPort))
c.Send(data, data.Length, dstIp, dstPort);
}
SendUdp(11000, "192.168.1.255", 11000, Encoding.ASCII.GetBytes("Olá, eu sou o Goku!"));
The above example sends a UDP packet to all hosts on the current subnet and port 11000.