0
我一直在網上搜索一些解決方案o我的問題,但我根本無法使255.255.255.255
廣播工作。Android廣播到255.255.255.255不工作
String broadip = "255.255.255.255";
DatagramSocket socket= new DatagramSocket(PORT);
InetAddress address = InetAddress.getByName(broadip);
socket.setReuseAddress(true);
socket.setBroadcast(true);
InetSocketAddress sAddress = new InetSocketAddress(broadip, PORT);
socket.bind(sAddress);
int length = this.buffer.length;
DatagramPacket packet = new DatagramPacket(this.buffer, length,address,PORT);
socket.send(packet);
socket.close();
當它到達socket.bind(sAddress)
線,它拋出一個異常「無法綁定」。我也嘗試「跳過」這個綁定行,套接字發送數據包,但其他機器永遠不會收到這個數據包。
奇怪的是下面的代碼工作得很好,但只將數據包發送到192.168.25.8
,我需要一個廣播,而不是:
String broadip = "192.168.25.8";
DatagramSocket socket= new DatagramSocket(PORT);
InetAddress address = InetAddress.getByName(broadip);
socket.setReuseAddress(true);
socket.setBroadcast(true);
int length = this.buffer.length;
DatagramPacket packet = new DatagramPacket(this.buffer, length,address,PORT);
socket.send(packet);
socket.close();
有Android清單權限:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
誰都可以幫幫我?
你可以試試這個ip來廣播:172.16.0.0看到https://en.wikipedia.org/wiki/Broadcast_address – nafas
https://stackoverflow.com/a/15913678/2657100 – nandsito
我的回答對你有幫助嗎? – Kaushal28