rdavisau / sockets-for-pcl

Cross-platform socket API for Xamarin iOS/Android/Forms, Xamarin.Mac/MonoMac, Windows Phone 8/8.1, Windows Store and Windows Desktop.
MIT License
224 stars 73 forks source link

Unable to receive udp multicast messages in android #121

Open betoled opened 7 years ago

betoled commented 7 years ago

Hi,

I'm not able to receive any multicast messages in Android. When running the UWP project everything is ok, but I can't get it to work when running the Android project. This is my receiver code :

private async Task DiscoverServer()
{
        // typical instantiation
        if (receiver == null)
        {
            var port = 9121;
            var address = "239.255.10.10"; // must be a valid multicast address

            receiver = new UdpSocketMulticastClient();
            receiver.TTL = 5;

            receiver.MessageReceived += Receiver_MessageReceived;

            // join the multicast address:port
            await receiver.JoinMulticastGroupAsync(address, port);
        }

        IPAddresses.Clear();
    }

private void Receiver_MessageReceived(object sender, Sockets.Plugin.Abstractions.UdpSocketMessageReceivedEventArgs args)
    {
        var from = String.Format("{0}:{1}", args.RemoteAddress, args.RemotePort);
        var data = Encoding.UTF8.GetString(args.ByteData, 0, args.ByteData.Length);

        if (data == "SERVER")
        {
            string serverAddress = args.RemoteAddress;
            IPAddresses.Add(serverAddress);
        }
    }

These are my manifest permissions :

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

Why is this not working ?

rdavisau commented 7 years ago

Howdy, can you list out a few more things to help diagnose :shipit:

betoled commented 7 years ago

Hi,

The sender is a server UWP application running on Windows IOT (Raspberry pi 3). It used your plugin to send a multicast message for clients to pickup. The idea is that clients can find all servers in a network.

The client is either my development computer when I'm testing the UWP client, or an Android cellphone when I'm testing the Android client. The UWP client works, but the Android client is not receiving the udp message. I do not use emulators. Both client and server are physical devices on the same network. When the user clickes a button the client app waits for server messages for 5 seconds and lists all IP addresses for incoming messages. Below is the code that calls the DiscoverServer method

private void BtnDiscover_Clicked(object sender, EventArgs args) { RunDiscovery();

        Task doList = Task.Factory.StartNew(async () =>
        {
            await Task.Delay(5000);
            Device.BeginInvokeOnMainThread(() =>
            {
                lvFoundServer.ItemsSource = IPAddresses;
            });
        });
    }

    private async void RunDiscovery()
    {
        try
        {
            await DiscoverServer();
        }
        catch (AggregateException ae)
        {
            foreach (Exception e in ae.InnerExceptions)
            {
                if (e is TaskCanceledException)
                    Debug.WriteLine("Unable to compute mean: {0}",

((TaskCanceledException)e).Message); else Debug.WriteLine("Exception: " + e.GetType().Name); } } }

Hope you can help. 2016-12-12 23:01 GMT+01:00 Ryan Davis notifications@github.com:

Howdy, can you list out a few more things to help diagnose [image: :shipit:]

  • platform and network of the device sending the udp packets
  • platform, physical vs emulator and network of the UWP device
  • physical vs emulator and network of the android device
  • what does the code that calls DiscoverServer look like

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rdavisau/sockets-for-pcl/issues/121#issuecomment-266566805, or mute the thread https://github.com/notifications/unsubscribe-auth/AH0fZhorsVfmpgOUaLIij9qdAMq068vKks5rHcQ9gaJpZM4LLCQh .

gcadmes commented 7 years ago

I'm having the same issue. The sockets-for-pcl works fine with Android 6. My co has me testing our UDP discovery service on a Nexus 9 physical device running android 7.1, and the app eventually crashes.

Something in my output I've never seen before was this error:

"JNI detected error in application use of deleted local reference 0x1"

Ryan, have you tried your PCL sockets code running Android 7.1 on a physical device? I'd like to help if I can. Perhaps I can grab latest and run the code locally to see where in your codebase the issue is. We really need this fixed asap.

thanks

clementmangin commented 7 years ago

Try acquiring a multicast lock:

WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.setReferenceCounted(true);
multicastLock.acquire();

...

multicastLock.release();

Credits to https://www.b4x.com/android/forum/threads/enable-multicast.43321/#post-263242