-1
10-28 14:40:11.530: W/System.err(11802): java.net.SocketException: setsockopt failed: ENODEV (No such device)
10-28 14:40:11.530: W/System.err(11802):    at libcore.io.IoBridge.setSocketOption(IoBridge.java:324)
10-28 14:40:11.530: W/System.err(11802):    at java.net.PlainDatagramSocketImpl.setOption(PlainDatagramSocketImpl.java:186)
10-28 14:40:11.530: W/System.err(11802):    at java.net.PlainDatagramSocketImpl.join(PlainDatagramSocketImpl.java:126)
10-28 14:40:11.530: W/System.err(11802):    at java.net.MulticastSocket.joinGroup(MulticastSocket.java:149)
10-28 14:40:11.530: W/System.err(11802):    at com.lsm.activity.TestActivity$MySendThread.run(TestActivity.java:145)
10-28 14:40:11.530: W/System.err(11802): Caused by: libcore.io.ErrnoException: setsockopt failed: ENODEV (No such device)
10-28 14:40:11.530: W/System.err(11802):    at libcore.io.Posix.setsockoptGroupReq(Native Method)
10-28 14:40:11.530: W/System.err(11802):    at libcore.io.ForwardingOs.setsockoptGroupReq(ForwardingOs.java:125)
10-28 14:40:11.530: W/System.err(11802):    at libcore.io.IoBridge.setSocketOptionErrno(IoBridge.java:397)
10-28 14:40:11.530: W/System.err(11802):    at libcore.io.IoBridge.setSocketOption(IoBridge.java:322)
10-28 14:40:11.530: W/System.err(11802):    ... 4 more
4

1 回答 1

-1

通过以下方式,我已经处理了这个问题。

Enumeration enumeration = NetworkInterface.getNetworkInterfaces();

NetworkInterface eth0 = null;

while (enumeration.hasMoreElements() {

eth0 = enumeration.nextElement()
if (eth0.getName().equals("eth0")) {

    //there is probably a better way to find ethernet interface
    break;
}

}

InetAddress group = InetAddress.getByName(IP);

MulticastSocket s = new MulticastSocket(PORT);

s.setSoTimeout(10000);

//s.joinGroup(group);
 //this will throw "No such device" exception

s.joinGroup(new InetSocketAddress(group, PORT), eth0);

// this works just fine

for (int i = 0; i < 10; ++i) {

byte[] buf = new byte[8096];

DatagramPacket recv = new DatagramPacket(buf, buf.length);

s.receive(recv);

System.out.println("Recieved " + recv.getLength() + " bytes.");

}

s.leaveGroup(group);
于 2015-10-28T10:37:51.483 回答