UDP Socket wont receive data on android

Я есть 96 Reputation points
2020-12-01T08:10:31.997+00:00

I have an issue with UdpClient.Receive(ref IPEndpoint) on android devices: for some reason the same code works on PC, but android devices (or emulators) have a 50% chance to get stuck on Receive() method.
It doesn't depend on networking type (wired, wifi connection, or cell network). Packet capture programs (such as Wireshark) show that packets are coming to device, and that the device receives it (tested on PC - Bluestacks, on android device - tested with PacketCapture). But for some reason the application won't process that data.
As example, I made a simple solution with two projects (pc server and android client application), you can watch it here, or check the test code: https://github.com/Duude92/UDP-ANDROID-TEST
Steps to reproduce: open solution, set your IP and Port, send message from android app to server (data should come), then send message from pc to application (50% chance here that application will wait for Receive() method forever, even when data has come to device, application didnt crashed, just paused on that method)
For example i added udp class using Java.Net, and it seems like working just fine.
Receive block of code below:

rEndpoint = new IPEndPoint(IPAddress.Any, 0);
ReceiveClient = new UdpClient();
ReceiveClient.ExclusiveAddressUse = false;
ReceiveClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
ReceiveClient.Client.Bind(rEndpoint);

private void receiveData()
{
    while (true)
    {
        byte[] data = new byte[256];
        data = ReceiveClient.Receive(ref remoteEndpoint);
        int bytess = data.Length;
        if (bytess == 0)
        {
            return;
        }
        textAdd.Enqueue(Encoding.Default.GetString(data));
    }
}
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,223 questions
0 comments No comments
{count} votes

Accepted answer
  1. Я есть 96 Reputation points
    2020-12-02T07:03:52.713+00:00

    Today i found the solution.
    The reason was misinformation/bad advice/bug?
    Most of lessons shows that you need two clients (one for receiveing, other for sending).
    To share same port, you need to set option for clients

    UdpClient.ExclusiveAddressUse = false;
    UdpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

    And it seems to be working on Windows, but not for Android.
    The solution is to use single UdpClient object for every operation on same port.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cheong00 3,471 Reputation points
    2020-12-02T03:45:30.277+00:00

    According to the documentation, if you set the port number to 0, UdpClient will bind to random port assigned by underlying service provider instead. In fact I checked Android API and unable to find any method that will allow you to listen to any port number. So unless the sending side happens to send UDP targeting the same port number, your application will not receive it.

    Try give it arbitrary port number > 1024 (on *nix system you require root access to bind to port > 1024) and see if it works. (remember to modify sending side to make it send to the same port number)

    Also, you may want to reduce the sending rate because the receiving side might drop the packets directly when the buffer is full.