One of the ways to understand how UDP works is by comparing it to TCP.
TCP
- Connection: To use the TCP protocol you need to establish a connection with one (and only one) host remote.
- Bi-directional communication: Once the connection is established, the two points can send messages (which is why IP tunneling works)
- Point-to-point: A connection involves only two points.
- Guaranteed transmission: Once a package has been sent to a remote host, the protocol monitors the delivery (or warns about failures).
UDP
- Unconnected: You do not establish connections when emitting UDP packets; you only specify a target.
- Unsecured: The protocol does not implement any kind of guarantee that the package will reach the target host.
- Masking: A package can be sent to multiple hosts at the same time when the target is a mask.
In the example below, an UDP package sent to the address 192.168.1.255
from the host 192.168.1.15 will be received by all the hosts of the subnet, inclusive 192.168.1.15; The Octet .255
equates, in general terms and in the given example, to 'all hosts under 192.168.1'.
Example of UDP package issuance (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 to all hosts of the current subnet and port 11000 a UDP package.
Kadu, are perfect for you: - http://www.codeproject.com/Tips/607801/SimpleplusChatplusprogramplusinpluplusC - http://sourceforge.net/projects/basic-chat-program/ - http://www.c-sharpcorner.com/UploadFile/97ec13/-howto-make-a-chat-application-C-harp/
– Felipe Douradinho
Felipe, thank you for the answer, I will look at the codes, but there is still an important part of the question that is "I’d like to know the logic behind this request", that is, I would like to know a little about the theoretical part of a UDP protocol, which I actually forgot to add in the question...
– KaduAmaral