简体   繁体   中英

c# udp client-server echo program

I just started socket programming in c#. I wanted to develop a simple client-server echo application. The problem I encountered is when i try to echo the message back to the client, it does not receive it. I spent a lot of time searching for solution on variety of forums but i couldn't find any that would help me with my problem.

Thanks in advance. Andrew

Here is the code:

Server:

    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

    }

I was able to get this working by making the client talk directly to the server instead of broadcasting:

var serverAddress = "127.0.0.1"; // Server is on the local machine
IPAddress address = IPAddress.Parse(serverAddress);

...unless I'm missing an important reason why you were using broadcast in your original code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM