Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过Wi-Fi直接广播_Java_Android_Broadcast_Socketexception_Wifi Direct - Fatal编程技术网

Java 通过Wi-Fi直接广播

Java 通过Wi-Fi直接广播,java,android,broadcast,socketexception,wifi-direct,Java,Android,Broadcast,Socketexception,Wifi Direct,我正在考虑在多个Android设备之间通过Wi-Fi直接连接进行广播的可能性。我已经创建了一个简单的消息广播应用程序来测试它是否工作,但到目前为止我还不能广播消息。当我尝试发送数据包时,我收到一个SocketException(无法访问网络): 这是我的代码的精髓: InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255"); int port = 8888; DatagramSocket socket = n

我正在考虑在多个Android设备之间通过Wi-Fi直接连接进行广播的可能性。我已经创建了一个简单的消息广播应用程序来测试它是否工作,但到目前为止我还不能广播消息。当我尝试发送数据包时,我收到一个SocketException(无法访问网络):

这是我的代码的精髓:

InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255");
int port = 8888;

DatagramSocket socket = new DatagramSocket(port);
socket.setBroadcast(true);
socket.connect(broadcastAddress, port);

String message = "Hello";
byte[] buffer = message.getBytes();

DatagramPacket packet = new DatagramPacket(
        buffer, buffer.length, broadcastAddress, port);

try {
    socket.send(packet); // <----- Causes a SocketException
} catch (IOException e) {
    Log.e(TAG, e.getMessage(), e);
}
我还尝试将
InetAddress.getByName(“0.0.0.0”)
作为参数添加到
DatagramSocket
,但没有成功


建议?

无耻地从

尝试通过以下方式获得网络连接:

InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
      quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}  
编辑:从同一页阅读尝试此

WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = wifi.createMulticastLock("dk.aboaya.pingpong");
lock.acquire();
serverSocket = new DatagramSocket(19876);
serverSocket.setSoTimeout(15000); //15 sec wait for the client to connect
byte[] data = new byte[UDPBatPositionUpdater.secretWord.length()]; 
DatagramPacket packet = new DatagramPacket(data, data.length);
serverSocket.receive(packet);
lock.release();
String s = new String(packet.getData());
System.out.println(s);
请记住,要使其工作,您需要以下权限:

使用该功能获取广播地址后,SocketException消失,广播似乎正常,但我在第二台设备上接收广播时遇到问题。我在接收设备上以相同的方式初始化一个
DatagramSocket
。我还尝试通过添加
InetAddress.getByName(“0.0.0.0”)
作为参数,在接收
DatagramSocket
上使用通配符地址。不走运。@user2190832在我的答案中添加了更多细节来阅读UDP。代码非常有用,谢谢!关于设备的一些细节:我构建了自己的库来处理基于此代码的通信。我目前正在使用8888端口。我的Nexus 5可以发送和接收。My Moto G只能发送软件包(并接收自己的软件包,但不能从其他设备接收)。我最近尝试了三星Galaxy S2,可以收发软件包。莫名其妙的是,摩托罗拉正在屏蔽接收到的广播。我尝试了两种不同的Moto G,一种是使用了特定的防火墙策略,另一种是库存,但没有成功。顺便说一句:看看这篇文章:
InetAddress getBroadcastAddress() throws IOException {
    WifiManager wifi = mContext.getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcp = wifi.getDhcpInfo();
    // handle null somehow

    int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
    byte[] quads = new byte[4];
    for (int k = 0; k < 4; k++)
      quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
    return InetAddress.getByAddress(quads);
}  
DatagramSocket socket = new DatagramSocket(PORT);
socket.setBroadcast(true);
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(),
    getBroadcastAddress(), PORT);
socket.send(packet);

// If you want to listen for a response ...
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
WifiManager wifi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
MulticastLock lock = wifi.createMulticastLock("dk.aboaya.pingpong");
lock.acquire();
serverSocket = new DatagramSocket(19876);
serverSocket.setSoTimeout(15000); //15 sec wait for the client to connect
byte[] data = new byte[UDPBatPositionUpdater.secretWord.length()]; 
DatagramPacket packet = new DatagramPacket(data, data.length);
serverSocket.receive(packet);
lock.release();
String s = new String(packet.getData());
System.out.println(s);