简体   繁体   English

UDP 客户端收不到任何数据

[英]UDP Client can't receive any data

I've started learning System.Net, and coded a simple UDP client-server chat application.我已经开始学习 System.Net,并编写了一个简单的 UDP 客户端-服务器聊天应用程序。

The client and server are both located on my machine (via local) for simplicity reasons.出于简单的原因,客户端和服务器都位于我的机器上(通过本地)。

I can send a message from client to server and write it there, but after that, the client should receive a hardcoded message too (Hello again,).我可以从客户端向服务器发送一条消息并将其写入那里,但在那之后,客户端也应该收到一条硬编码的消息(你好,再次,)。 which is my problem.这是我的问题。

Screenshot of application (server and client)应用程序(服务器和客户端)的屏幕截图

Client code:客户端代码:

public class UDPClient
        {
            private Socket clientSocket;
            private EndPoint epSend, epRecieve;

            private byte[] dataBuffer;
            private ArraySegment<byte> dataBufferSegments;

            /// <summary>
            /// initializing client with buffer, end points of server and client (for sending and listening)
            /// </summary>
            /// <param name="address"></param>
            /// <param name="port">for future port-forwarding</param>
            public void Initialize(IPAddress address, int port = 0)
            {
                dataBuffer = new byte[4096];
                dataBufferSegments = new ArraySegment<byte>(dataBuffer);

                epRecieve = new IPEndPoint(address, clientListenOn);
                epSend = new IPEndPoint(address, serverListenOn);

                Console.WriteLine(epRecieve.Serialize());
                Console.WriteLine(epSend.Serialize());

                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                clientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
            }

            /// <summary>
            /// async task for listening on packets
            /// </summary>
            public void Listen()
            {
                _ = Task.Run(async () =>
                {
                    SocketReceiveMessageFromResult res;
                    while (true)
                    {
                        res = await clientSocket.ReceiveMessageFromAsync(dataBufferSegments, SocketFlags.None, epRecieve);

                        Console.WriteLine(Encoding.UTF8.GetString(dataBuffer, 0, bufferSize));
                    }
                });
            }
            /// <summary>
            /// async task for sending data back to server
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public async Task Send(byte[] data)
            {
                var s = new ArraySegment<byte>(data);
                await clientSocket.SendToAsync(s, SocketFlags.None, epSend);
            }
        }

Server code:服务器代码:

    /// <summary>
    /// UDP Server
    /// </summary>
    public class UDPServer
    {
        private Socket serverSocket;
        private EndPoint epRecieve, epSend;

        private byte[] dataBuffer;
        private ArraySegment<byte> dataBufferSegments;

        public void Initialize()
        {
            dataBuffer = new byte[4096];
            dataBufferSegments = new ArraySegment<byte>(dataBuffer);

            epRecieve = new IPEndPoint(IPAddress.Loopback, serverListenOn);

            Console.WriteLine(epRecieve.Serialize());

            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            serverSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
            serverSocket.Bind(epRecieve);
        }

        public void Listen()
        {
            _ = Task.Run(async () =>
            {
                SocketReceiveMessageFromResult res;
                while (true)
                {
                    res = await serverSocket.ReceiveMessageFromAsync(dataBufferSegments, SocketFlags.None, epRecieve);
                    Console.WriteLine(Encoding.UTF8.GetString(dataBuffer, 0, bufferSize));

                    Console.WriteLine(res.RemoteEndPoint);
                    Console.WriteLine(res.ReceivedBytes.ConvertToString());
                    Console.WriteLine(res.PacketInformation.Address);

                    epSend = new IPEndPoint(res.PacketInformation.Address, clientListenOn);

                    Console.WriteLine(epSend.Serialize());

                    //this doesn't seem to work...
                    await SendTo(epSend, Encoding.UTF8.GetBytes("Hello back!"));
                }
            });
        }

        public async Task SendTo(EndPoint recipient, byte[] data)
        {
            var s = new ArraySegment<byte>(data);
            await serverSocket.SendToAsync(s, SocketFlags.None, recipient);
        }
    }

Server main code:服务器主要代码:

static void Main(string[] args)
        {
            Network.UDPServer server = new Network.UDPServer();
            server.Initialize();
            server.Listen();

            Console.ReadLine();
        }

Client main code:客户端主代码:

static async Task Main(string[] args)
        {
            Network.UDPClient client = new Network.UDPClient();

            IPAddress ip = IPAddress.Loopback;
            Console.WriteLine(ip.ToString());

            client.Initialize(ip);
            client.Listen();
            await client.Send(Encoding.UTF8.GetBytes(Console.ReadLine()));

            Console.ReadLine();
        }

Thus I would like anybody to pinpoint out what's wrong with my system.因此,我希望任何人都能查明我的系统出了什么问题。

Much appreciate!非常感谢!

I pasted your code on Visual Studio and with minor monifications (some missing fields etc), the problem I came across is the following:我将您的代码粘贴到 Visual Studio 上并进行了一些小修改(一些缺少字段等),我遇到的问题如下:

On your UDP client, this line throws an exception of type InvalidOperationException .在您的 UDP 客户端上,此行会引发InvalidOperationException类型的异常。

System.InvalidOperationException: 'You must call the Bind method before performing this operation.'
res = await clientSocket.ReceiveMessageFromAsync(dataBufferSegments, SocketFlags.None, epRecieve);

You need to bind the socket to a local endpoint, specifically your receive endpoint.您需要将套接字绑定到本地端点,特别是您的接收端点。 This is done in order to associate that socket with that specific endpoint and receive data from it.这样做是为了将该套接字与该特定端点相关联并从中接收数据。

            public void Initialize(IPAddress address, int port = 0)
            {
                dataBuffer = new byte[4096];
                dataBufferSegments = new ArraySegment<byte>(dataBuffer);

                epRecieve = new IPEndPoint(address, clientListenOn);
                epSend = new IPEndPoint(address, serverListenOn);

                Console.WriteLine(epRecieve.Serialize());
                Console.WriteLine(epSend.Serialize());

                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                clientSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
                _clientSocket.Bind(_epRecieve);
            }

You can read more here: MSDN - Socket.Bind(EndPoint) Method您可以在此处阅读更多信息: MSDN - Socket.Bind(EndPoint) 方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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