Multicast : using joinGroup on a multicast socket

2019-07-10 19:39发布

I'm developping an android app (using java): i'm trying to send informations between several phones using java MulticastSocket. These phones connect to the same access point when the app is launched, this access point is my computer. This works well because when the app was launched, i can access the internet. But when i'm trying to do the joinGroup on my socket, i get the error : java.net.SocketException: setsockopt failed: ENODEV (No such device)

I already did a lot of research about this but no result matches with my problem.

Here is my code :

In the main activity, where i'm connecting the phones to the same hotspot.

msg = handlerConnectionMsg.obtainMessage();
        Bundle b = new Bundle();

        // Get the wifi manager
        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        WifiManager.MulticastLock multicastLock = wifiManager.createMulticastLock("mydebuginfo");
        multicastLock.setReferenceCounted(true);
        multicastLock.acquire();

        // Enable the wifi
        boolean wasEnabled = wifiManager.isWifiEnabled();

        msg = handlerConnectionMsg.obtainMessage();
        b = new Bundle();
        b.putInt("connection_message", ACTIVATING_WIFI);
        msg.setData(b);
        handlerConnectionMsg.sendMessage(msg);

        wifiManager.setWifiEnabled(true);
        // Wait for the connection
        while(!wifiManager.isWifiEnabled());

        boolean ret = true;

        // Connect to the access point and disable the connection to others
        if (wifiManager.isWifiEnabled() && wifiManager.startScan())
        {
            msg = handlerConnectionMsg.obtainMessage();
            b = new Bundle();
            b.putInt("connection_message", CONNECTION_AP);
            msg.setData(b);
            handlerConnectionMsg.sendMessage(msg);

            ret = connectToAP(wifiManager, "trainee_hotspot_test", "d0nt3nt3rThis");
        }

        if (!ret)
        {
            msg = handlerConnectionMsg.obtainMessage();
            b = new Bundle();
            b.putInt("connection_message", CONNECTION_ERROR);
            msg.setData(b);
            handlerConnectionMsg.sendMessage(msg);

            return;
        }

        msg = handlerConnectionMsg.obtainMessage();
        b = new Bundle();
        // Valid connection
        b.putInt("connection_message", CONNECTED);
        b.putString("current_ap", wifiManager.getConnectionInfo().getSSID());
        msg.setData(b);
        handlerConnectionMsg.sendMessage(msg);
    }

    private boolean connectToAP(WifiManager wifi, String name, String pass)
    {
        // Create a new wifiConfiguration object
        WifiConfiguration wifiConfig = new WifiConfiguration();

        // Add informations to it
        wifiConfig.SSID = String.format("\"%s\"", name);
        wifiConfig.preSharedKey = String.format("\"%s\"", pass);

        // We add this configuration to the wifi manager and get the id
        int netId = wifi.addNetwork(wifiConfig);
        // Disconnect from the current AP
        wifi.disconnect();
        // Enable the hotspot with given SSID if it exists
        boolean status = wifi.enableNetwork(netId, true);
        wifi.reconnect();

        return status;
    }
}

These two lines in the class which create the multicast socket and handle the data receiving.

multicastSocket = new MulticastSocket(port);
multicastSocket.joinGroup(getGroup());

The port is 4321 andthe multicast address (returned by getGroup ) is 224.1.1.1

I don't understand why this doesn't work, it worked well when i was using multicast socket for computer.

Thanks all, havea good day

**Update : **

I remove the error by replacing :

multicastSocket.joinGroup(getGroup());

By :

        multicastSocket.joinGroup(new InetSocketAddress(getGroup(), port), NetworkInterface.getByName("p2p0"));

This removed the error but my multicasting isn't working. The packet are sent but are not received.

0条回答
登录 后发表回答