2

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.

Rikoo
  • 31
  • 4
  • What does 'it worked well when i was using multicast socket for computer' mean? – user207421 Apr 13 '17 at 09:52
  • I already used multicast socket for computers and it worked well. When i sent data, it was received by the other computer which was listening. And i didn't got the error code when i did the joinGroup. – Rikoo Apr 13 '17 at 10:54

1 Answers1

0

I know this issue is so old but these days I'm having problems to send multicast packets from one interface(eth0) to over other Interface(usbnet0) (I have 2 ethernet in my Odroid UX4, usb hub+ethernet) and I'm using the code like you but not working, always send the packets to default interface "eth0"

WRONG CODE;

NetworkInterface nif2 = NetworkInterface.getByName("usbnet0");

MulticastSocket mcs2 = new MulticastSocket(dPort);

mcs2.joinGroup(new InetSocketAddress(dIP, dPort), nif2);

But I only had to set the correct interface using another method

RIGHT CODE;

NetworkInterface nif2 = NetworkInterface.getByName("usbnet0");

MulticastSocket mcs2 = new MulticastSocket(dPort);

mcs2.setNetworkInterface(nif2);  //THIS LINE IS THE KEY

mcs2.joinGroup(new InetSocketAddress(dIP, dPort), nif2);

Regards

Jorge Garcia
  • 81
  • 1
  • 3