Discover Broadcast address for sending UDP message

2

My Wi-Fi network interface contains the following settings:

Wireless LAN adapter Wi-Fi:

Connection-specific DNS Suffix  . : home
Link-local IPv6 Address . . . . . : fe80::95d7:bda0:eac3:ebf7%5
IPv4 Address. . . . . . . . . . . : 192.168.1.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.1.1

One way I send a text stream is by using the UDP protocol.

using System;
using System.Net;
using System.Net.Sockets;using System.Text;

class Program
{
    static void Main(string[] args)
    {
    Socket emissor = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPAddress destinatario = IPAddress.Parse("192.168.1.255");
    IPEndPoint emissor_end_point = new IPEndPoint(destinatario, 33333);

    Console.WriteLine("Digite a mensagem a enviar:");
    string mensagem = Console.ReadLine();
    byte[] mensagem_buffer = Encoding.ASCII.GetBytes(mensagem);

    emissor.SendTo(mensagem_buffer, emissor_end_point);
    emissor.Close();

    Console.WriteLine("Mensagem enviada ...");
    Console.ReadKey();
    }
}

In the case of a class C ip address, in order to send the message in broadcast just change the last octet to 255.

Is it possible to get this ip address automatically, regardless of the network where the program runs?

I wanted to avoid the "fastidious" process of rendering text with the strings associated with ip and its mask.

    
asked by anonymous 28.05.2016 / 16:17

2 answers

3
using System;
using System.Net;
using System.Net.Sockets;using System.Text;

class Program
{
    static void Main(string[] args)
    {
    Socket emissor = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    // IPAddress destinatario = IPAddress.Parse("192.168.1.255");
    udpSocket.EnableBroadcast = true;
    IPAddress destinatario = IPAddress.Broadcast
    IPEndPoint emissor_end_point = new IPEndPoint(destinatario, 33333);

    Console.WriteLine("Digite a mensagem a enviar:");
    string mensagem = Console.ReadLine();
    byte[] mensagem_buffer = Encoding.ASCII.GetBytes(mensagem);

    emissor.SendTo(mensagem_buffer, emissor_end_point);
    emissor.Close();

    Console.WriteLine("Mensagem enviada ...");
    Console.ReadKey();
    }
}

Refers to Socket.EnableBroadcast Property , IPAddress.Broadcast Field

Another example of poorly typed code taken from the MSDN to receive UDP datagrams

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class UDPListener 
{
    private const int listenPort = 11000;

    private static void StartListener() 
    {
        bool done = false;

        UdpClient listener = new UdpClient(listenPort);
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any,listenPort);

        try 
        {
            while (!done) 
            {
                Console.WriteLine("Waiting for broadcast");
                byte[] bytes = listener.Receive( ref groupEP);

                Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                    groupEP.ToString(),
                    Encoding.ASCII.GetString(bytes,0,bytes.Length));
            }

        } 
        catch (Exception e) 
        {
            Console.WriteLine(e.ToString());
        }
        finally
        {
            listener.Close();
        }
    }

    public static int Main() 
    {
        StartListener();

        return 0;
    }
}
    
28.05.2016 / 17:28
1

I leave my final version of the elemental receiver / receiver.

Issuer

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        //Declarações
        int porto = 33333;
        Socket emissor = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        IPAddress destinatario = IPAddress.Broadcast;
        emissor.EnableBroadcast = true;
        IPEndPoint emissorEndPoint = new IPEndPoint(destinatario, porto);

        // Enviar mensagem
        Console.WriteLine("Digite a mensagem a enviar:");
        string mensagem = Console.ReadLine();
        byte[] mensagemBuffer = Encoding.ASCII.GetBytes(mensagem);
        emissor.SendTo(mensagemBuffer, emissorEndPoint);
        emissor.Close();

        //Terminar ...
        Console.WriteLine("Mensagem enviada\nPrima uma tecla para continuar ...");
        Console.ReadKey();
    }
}

Recipient

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

public class Program
{
    public static int Main()
    {
        //Declarações
        int porto = 33333;
        UdpClient recetor = new UdpClient(porto);
        IPEndPoint escutaEndPoint = new IPEndPoint(IPAddress.Any, porto);
        string mensagem;
        byte[] recebidoByteArray;

        //Receção
        Console.WriteLine("A aguardar chegada de mensagem em broadcast...");
        recebidoByteArray = recetor.Receive(ref escutaEndPoint);
        mensagem = Encoding.ASCII.GetString(recebidoByteArray, 0, recebidoByteArray.Length);
        Console.WriteLine("[Mensagem recebida a {0} ] {1}", DateTime.Now, mensagem);
        recetor.Close();

        // Terminar ...
        while (Console.KeyAvailable) Console.ReadKey(false);
        Console.WriteLine("Prima uma tecla para continuar ...");
        Console.ReadKey();
        return 0;
    }
}
    
28.05.2016 / 18:28