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