C#UDP Server与客户端通信

时间:2014-09-29 23:51:57

标签: c# sockets networking udp

我尝试了多种方法,而且似乎没有成功。但必须有办法。

我正在尝试(在C#中)创建一个服务器。我希望服务器监听IP和端口,当它连接到客户端时,我希望它读取客户端说的内容,并发送回复。对于客户端,我想连接到服务器并发送数据,并从服务器接收回复。

我该怎么做呢?

我使用过MSDN上的Microsoft示例和示例。他们有客户>数据>服务器,但它似乎似乎不包括服务器回复。

我知道这可以做到,显然,因为我们有多人游戏。

感谢您的帮助。

编辑 - 代码段

SERVER

static void Main(string[] args)
        {
            int recv;
            byte[] data = new byte[1024];

            IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 904);
            Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            newSocket.Bind(endPoint);
            Console.WriteLine("Listening for connections...");

            //LISTEN FOR CLIENT
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 904);
            EndPoint tmpRemote = (EndPoint)sender;

            //READ MESSAGE FROM CLIENT
            recv = newSocket.ReceiveFrom(data, ref tmpRemote);
            Console.WriteLine("Messaged received from: " + tmpRemote.ToString());
            Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

            string welcome = "Welcome to server!";
            data = Encoding.ASCII.GetBytes(welcome);

            //SEND WELCOME REPLY TO CLIENT
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sock.Bind(tmpRemote);
            sock.SendTo(data, tmpRemote);
            Console.WriteLine("Reply sent to client"); 
            
                

            while (true)
            {
                if(!newSocket.Connected)
                {
                    Console.WriteLine("Client disconnected.");
                    break;
                }

                data = new byte[1024];
                recv = newSocket.ReceiveFrom(data, ref tmpRemote);
                if (recv == 0)
                    break;

                Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));

            }

            newSocket.Close();
            Console.WriteLine("Server disconnected.");
            Console.ReadLine();

        }
    }

客户端

 static void Main(string[] args)
        {
            Console.WriteLine("Message [127.0.0.1:904]: ");
            string msg = Console.ReadLine();
            byte[] packetData = ASCIIEncoding.ASCII.GetBytes(msg);
            string IP = "127.0.0.1";
            int port = 904;

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            client.SendTo(packetData, ep);
            Console.WriteLine("Data sent!");


            int recv;
            byte[] data = new byte[1024];
            EndPoint tmpRemote = (EndPoint)ep;

           while(true)
           {
               //READ MESSAGE FROM SERVER
               recv = client.ReceiveFrom(data, ref tmpRemote);
               Console.WriteLine("Messaged received from: " + tmpRemote.ToString());
               Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
           }

           client.Close();
           Console.WriteLine("Client disconnected.");
           Console.ReadLine();

        }

我无法让服务器与客户端通话并让客户端读取/显示服务器的回复。

1 个答案:

答案 0 :(得分:1)

改变这句话

sock.SendTo(data, tmpRemote);

newSocket.SendTo(data, tmpRemote);

并删除这些句子,你已经绑定了两次本地EndPoint。

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.Bind(tmpRemote);

在网络中,您可以使用UdpClient代替Socket

如果您想要演示,请查看此demo