没有收到任何数据

没有收到任何数据

本文介绍了UdpClient.Receive(...) 没有收到任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套接字服务器(用 C++ 编写),它接收来自客户端的请求并将响应发回.所以,我的测试客户端应用程序 (C#) 非常简单:

I have a socket server (written on C++) which receives requests from clients and sends responses back. So, my test client application (C#) is very simple:

        try {
            UdpClient udpClient = new UdpClient(10241);

            // Connect
            udpClient.Connect(server_ip_address,  10240);

            // prepare the packet to send
            iWritableBuff wbuff = new iWritableBuff();
            [ ... ]

            // Send
            udpClient.Send(wbuff.GetBuff(), (int)wbuff.Written());

            // Receive a response
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
            Console.WriteLine("We've got some data from the server!!! " + receiveBytes.Length + " bytes.");

            udpClient.Close();
        } catch (Exception e) {
            Console.WriteLine("ERROR: " + e.ToString());
        }

我在同一台计算机上运行这两个应用程序.服务器从客户端端口 10241 接收到端口 10240 的请求并将响应发送回客户端端口 10241,但客户端从未收到它.所以,我确信服务器会发回数据包,因为一切都与 C++ 客户端完美配合.这意味着我在我的 C# 客户端上做错了.有什么想法吗?

I run both applications on the same computer. Server receives a request to port 10240 from the client port 10241 and sends a response back to client port 10241, but client never receive it. So, I'm sure that server sends packet back, because everything works perfectly with C++ client. It means that I'm, doing something wrong on my C# client. Any idea?

谢谢!

P.S.> 只需使用 Berkley Socket C# 客户端进行测试:

P.S.> Just test it with Berkley Socket C# client:

        try {
            // Create socket
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // Bind
            IPEndPoint myEP = new IPEndPoint(IPAddress.Any, 0);
            s.Bind(myEP);

            // prepare the packet to send
            iWritableBuff wbuff = new iWritableBuff();
            [ ... ]

            // Send it
            IPEndPoint sEP = new IPEndPoint(IPAddress.Parse(server_ip_address), 10240);
            int res = s.SendTo(wbuff.GetBuff(), (int)wbuff.Written(), 0, sEP);

            // Receive the response
            byte[] receiveBytes = new Byte[1024];
            EndPoint recEP = new IPEndPoint(IPAddress.Any, 0);
            res = s.ReceiveFrom(receiveBytes, ref recEP);
            Console.WriteLine("We've got some data from the server!!! " + res + " bytes.");
        } catch (Exception e) {
            Console.WriteLine("ERROR: " + e.ToString());
        }

它完美无缺!UdpSocket 有什么问题?

And it works perfect! What's is wrong with UdpSocket?

推荐答案

不确定这是否适用于不广播数据包而只是将数据包发送到指定地址的 UDP 客户端,但我遇到了类似的障碍.

Not sure if this applies to UDPClients that don't broadcast packets but simply send one to a specified address, but I ran into a similar roadblock.

我有一个 UDPClient 广播一个 udp 数据包,用于发现我们网络上的一些自定义机器.当我尝试接收服务器只会回显的消息时,它不会接收到信息并且会超时.事实证明,如果您使用广播消息的 UDPClient,它将无法接收消息.除了我幸运地在 msdn 上遇到的一个论坛主题外,任何地方都没有记录.

I had a UDPClient that broadcasted a udp packet for discovery of some custom machines on our network. When I tried to receive the message that the servers would simply echo back, it would not receive the information and would timeout. Turns out that if you use a UDPClient that broadcasts a message out, it WILL NOT be able to receive messages back. It's not documented anywhere, except for one forum topic on msdn that I luckily came across.

解决方案是发送消息,立即关闭套接字,在同一端口上打开一个新的 UDPClient,然后使用这个新的 UDPClient 接收回显的 UDP 数据包.很烦人.

The solution was to send the message, immediately close the socket, open a NEW UDPClient on the same port, then use this new UDPClient to receive the echoed back UDP packet. Very annoying.

试一试,看看它是否有效.它绝对适用于发送广播数据包.

Give that a shot and see if it works. It definitely works for sending out a broadcasted packet.

这篇关于UdpClient.Receive(...) 没有收到任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 10:41