接收中的异常:java.net.SocketException:没有这样的设备。在Android多播中

时间:2014-12-27 16:57:32

标签: java android udp multicast

我正在尝试连接到多播组并将udp数据包从服务器发送到客户端。

我在清单上写了正确的权限

服务器代码:

private DatagramSocket datagramSocket;
datagramSocket = new DatagramSocket();
InetAddress IPAddress;
IPAddress = InetAddress.getByName("230.0.0.1");
DatagramPacket sendPacket;
sendPacket = new DatagramPacket(data, data.length, IPAddress, port);
datagramSocket.send(sendPacket);

客户端代码:

private MulticastSocket soketmulNHC;
soketmulNHC = new MulticastSocket(8900);
soketmulNHC = new MulticastSocket(8900);
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
multicastLock.acquire();
byte[] buffer = new byte[Data.MAX_PACKET_SIZE];             
InetAddress group = InetAddress.getByName("230.0.0.1");                     
soketmulNHC.joinGroup(group);
DatagramPacket receivePacket = new DatagramPacket(buffer,buffer.length,group,8900);                             
soketmulNHC.receive(receivePacket);

我得到了这个例外:

Exception in recieving
java.net.SocketException: No such device
at org.apache.harmony.luni.platform.OSNetworkSystem.setSocketOption(Native Method)
at org.apache.harmony.luni.net.PlainDatagramSocketImpl.setOption(PlainDatagramSocketImpl.java:198)
at org.apache.harmony.luni.net.PlainDatagramSocketImpl.join(PlainDatagramSocketImpl.java:137)
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:190)

我还尝试从此链接Multicast - no such device添加界面部分,然后我得到了:

SocketException in sending: java.net.SocketException: Network is unreachable

使用S2 galaxy进行多播可能存在问题吗? 我需要你的帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

对于大多数设备(如果它们有多个网络设备),您需要明确选择应该使用哪个网络设备加入组播组。