c# udp client-serveur echo le programme

Je viens de commencer la programmation socket en c#. Je voulais créer un simple client-serveur, l'application d'écho. Le problème que j'ai rencontré, c'est quand j'essaie de l'écho du message de retour pour le client, il ne l'a pas reçu. J'ai passé beaucoup de temps à chercher une solution sur divers forums, mais je ne pouvais pas trouver n'importe qui pourrait m'aider avec mon problème.

Merci à l'avance.
Andrew

Voici le code:

Serveur:

    static void Main(string[] args)
    {

        string data = "";

        UdpClient server = new UdpClient(8008);


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


        Console.WriteLine(" S E R V E R   IS   S T A R T E D ");
        Console.WriteLine("* Waiting for Client...");
        while (data != "q")
        {
            byte[] receivedBytes = server.Receive(ref remoteIPEndPoint);
            data = Encoding.ASCII.GetString(receivedBytes);
            Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");
            Console.WriteLine("Message Received " + data.TrimEnd());

            server.Send(receivedBytes, receivedBytes.Length,remoteIPEndPoint);
            Console.WriteLine("Message Echoed to" + remoteIPEndPoint + data);
        }

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

Client:

    static void Main(string[] args)
    {


        string data = "";
        byte[] sendBytes = new Byte[1024];
        byte[] rcvPacket = new Byte[1024];
        UdpClient client = new UdpClient();
        IPAddress address = IPAddress.Parse(IPAddress.Broadcast.ToString());
        client.Connect(address, 8008);
        IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 0);

        Console.WriteLine("Client is Started");
        Console.WriteLine("Type your message");

        while (data != "q")
        {
            data = Console.ReadLine();
            sendBytes = Encoding.ASCII.GetBytes(DateTime.Now.ToString() + " " + data);
            client.Send(sendBytes, sendBytes.GetLength(0)); 
            rcvPacket = client.Receive(ref remoteIPEndPoint);

            string rcvData = Encoding.ASCII.GetString(rcvPacket);
            Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");

            Console.WriteLine("Message Received: " + rcvPacket.ToString());
        }
        Console.WriteLine("Close Port Command Sent");  //user feedback
        Console.ReadLine();
        client.Close();  //close connection

    }
Avez-vous essayer avec 2 ordinateurs ?
Malheureusement, j'ai seulement eu 1 ordinateur.
Je pense donc que vous ne pouvez pas le faire, s'il vous Plaît essayez de votre programme dans les 2 ordinateurs différents.
bien sûr, il peut le faire sur 1 ordinateur!
Qu'est ce que je pensais.

OriginalL'auteur user2226679 | 2013-03-30