多播:在多播套接字(java/android)数据包上使用joinGroup不是';未收到

多播:在多播套接字(java/android)数据包上使用joinGroup不是';未收到,java,android,sockets,multicastsocket,Java,Android,Sockets,Multicastsocket,我正在开发一个android应用程序(使用java):我正在尝试使用java多播套接字在多部手机之间发送信息。 这些手机在应用程序启动时连接到同一接入点,该接入点就是我的电脑。这很有效,因为当应用程序启动时,我可以访问互联网。 但是,当我尝试在套接字上执行joinGroup时,会出现以下错误: java.net.SocketException:setsockopt失败:ENODEV(无此类设备) 我已经对此做了很多研究,但没有一个结果与我的问题相符 这是我的密码: 在主要活动中,我将手机连接到同

我正在开发一个android应用程序(使用java):我正在尝试使用java多播套接字在多部手机之间发送信息。 这些手机在应用程序启动时连接到同一接入点,该接入点就是我的电脑。这很有效,因为当应用程序启动时,我可以访问互联网。 但是,当我尝试在套接字上执行joinGroup时,会出现以下错误: java.net.SocketException:setsockopt失败:ENODEV(无此类设备)

我已经对此做了很多研究,但没有一个结果与我的问题相符

这是我的密码:

在主要活动中,我将手机连接到同一个热点

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;
    }
}
类中的这两行代码创建多播套接字并处理数据接收

multicastSocket = new MulticastSocket(port);
multicastSocket.joinGroup(getGroup());
端口为4321,多播地址(由getGroup返回)为224.1.1.1

我不明白为什么这不起作用,当我在计算机上使用多播套接字时,它工作得很好

谢谢大家,祝你们今天愉快

**更新:**

我通过替换以下内容来删除错误:

multicastSocket.joinGroup(getGroup());
作者:


这消除了错误,但我的多播无法工作。数据包已发送但未收到。

我知道这个问题太老了,但最近我在通过另一个接口(usbnet0)将多播数据包从一个接口(eth0)发送到另一个接口(usbnet0)时遇到了问题(我的Odroid UX4中有2个以太网,usb集线器+以太网),我使用的代码与您一样,但不起作用,始终将数据包发送到默认接口“eth0”

错误代码

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

MulticastSocket mcs2 = new MulticastSocket(dPort);

mcs2.joinGroup(new InetSocketAddress(dIP, dPort), nif2);
但我只需要使用另一种方法设置正确的接口

权利代码

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

关于

什么是“我在计算机上使用多播套接字时工作得很好”的意思?我已经在计算机上使用了多播套接字,它工作得很好。当我发送数据时,另一台正在监听的计算机接收到了数据。我做joinGroup时没有得到错误代码。
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);