Sending a message to a local network broadcast via UDP

Asked

Viewed 2,933 times

3

I made a question about P2P connection, then with one of the answers the question arose: How to send a message on a local network via UDP in style broadcast, without a specific recipient?

With this I would like the following solutions:

  • How to find a possible recipient using UDP in style Broadcast?
  • How to send messages via UDP?
  • What is the logic behind the "style" Broadcast and how it is done using UDP protocol.

If possible with an example in C#.

  • 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, 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...

1 answer

3


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).

inserir a descrição da imagem aqui

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'.

inserir a descrição da imagem aqui

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.

  • Ono, is this mask the same as in the Windows network configurator? There when I put a fixed network IP, it automatically fills the field "Ipv4 Subnet Mask", then to send a package to a network I send to that mask?

  • @Kaduamaral The interpretation of the value is 'almost' the same, in the sense of allowing segmentation. But subnet is a completely different concept. It is worth taking a look at the definition: https://pt.wikipedia.org/wiki/M%C3%A1scara_de_rede

Browser other questions tagged

You are not signed in. Login or sign up in order to post.