UDP responder responds to client

I found this wonderful code on MSDN for the client / server UDP connection, however, the client can only send the server, it cannot respond to it. How can I do this so that the server can respond to the client who sent the message. Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;


namespace UDP_Server
{
    class Program
    {
        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;
        }
    }

}

And customer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Security.Cryptography;

namespace UDP_Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Send("TEST STRING");
            Console.Read();
        }
        static void Send(string Message)
        {
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress broadcast = IPAddress.Parse("10.1.10.117");
            byte[] sendbuf = Encoding.ASCII.GetBytes(Message);
            IPEndPoint ep = new IPEndPoint(broadcast, 11000);
            s.SendTo(sendbuf, ep);
        }
    }
}
+5
source share
2 answers

Just do it the other way around. Call StartListeneron the client and it can receive udp data, such as a server.

On your server, just send the data with the client code.

+8
source

, . - , .

0

All Articles