Socket UDP Send and Receive C #

1

I'm developing a C # application that uses UDP, the problem is that I can not respond to the client. I get the following error: Additional information: A request to send or receive data was not allowed because the socket is not connected and (when sending to a datagram socket using a sendto call) an address was not provided. p>

Follow the system: Server:

string data = "";
            byte[] d = new byte[1024];

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


            IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 6969);


            Console.WriteLine(" S E R V E R   IS   S T A R T E D ");
            Console.WriteLine("* Waiting for Client...");
            server.Bind(remoteIPEndPoint);
            while (data != "q") {
                //data = Console.ReadLine();

                server.Receive(d);
                data = Encoding.ASCII.GetString(d);
                Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");
                Console.WriteLine("Message Received " + data.TrimEnd());

                d = Encoding.ASCII.GetBytes("oi");
                server.Send(d);
                Console.WriteLine("Message sended to" + remoteIPEndPoint + " " + data);

            }

            Console.WriteLine("Press Enter Program Finished");
            Console.ReadLine(); //delay end of program
            server.Close();  //close the connection

Customer

string data = "";
        byte[] d = new byte[1024];

        Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


        IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969);


        Console.WriteLine(" C L I E N T   IS   S T A R T E D ");

        server.Connect(remoteIPEndPoint);

        Console.WriteLine("* Server connected...");

        while (data != "q") {
            d = Encoding.ASCII.GetBytes("teste");
            server.Send(d);
            Console.WriteLine("Message sended to" + remoteIPEndPoint + " " + data);



            server.Receive(d); 
            data = Encoding.ASCII.GetString(d);
            Console.WriteLine("Received from server: "+data);


        }

        Console.WriteLine("Press Enter Program Finished");
        Console.ReadLine(); //delay end of program
        server.Close();  //close the connection
    
asked by anonymous 04.03.2015 / 20:00

1 answer

2

UDP is a protocol that does not require a connection, unlike TCP. What you need is an endpoint that will receive the package.

Your code on the client may contain something similar to the following:

//Prepara um socket para ser utilizado como emissor
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

//Prepara o endereço-alvo
IPEndPoint endPointServidor = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969);

//Transforma o conteúdo a ser enviado em um array de bytes
byte[] d = Encoding.ASCII.GetBytes("teste");

//Envia o conteúdo [d] para o servidor [endPointServidor]
server.SendTo(d, endPointServidor);

Your server may then contain code similar to this:

//Prepara um socket para ser utilizado como receptor
Socket receiver = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

//Prepara o cliente Udp, ouvindo na porta 6969
UdpClient listener = new UdpClient(6969);

//Inicia a captura de pacotes. O metodo gerenciaRetorno sera chamado
//  sempre que um conteúdo via UDP chegar ao servidor
receiver.BeginReceive(gerenciaRetorno, listener);

    private static void gerenciaRetorno(IAsyncResult li)
    {
        IPEndPoint recEP = new IPEndPoint(IPAddress.Any, 0);
        Byte[] buffer = ((UdpClient)li.AsyncState).EndReceive(li, ref recEP);

        //[mensagem] conterá o conteúdo recebido.
        string mensagem = ASCIIEncoding.ASCII.GetString(buffer);

        //re-arma o socket para recepção
        receiver.BeginReceive(gerenciaRetorno, li.AsyncState);
    }

Source: C # UDP Socket client and server , original OS .

    
04.03.2015 / 20:06