Sending a message to a local network in the broadcast style via UDP

3

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

With this I would like the following solutions:

  • How do I find a potential recipient using UDP in the Broadcast style?
  • 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 #.

    
asked by anonymous 24.06.2015 / 23:06

1 answer

3

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.255fromhost192.168.1.15willbereceivedbyallhostsofthesubnet,including192.168.1.15;Theoctet.255isequivalent,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.

    
25.06.2015 / 14:38