简体   繁体   中英

UdpClient.Receive(…) doesn't receive any data

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());
        }

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?

Thanks!

PS> 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());
        }

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

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.

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.

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.

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