尝试播放udp数据包时遇到问题。
08-22 16:36:13.359 E/UDPTester.UDPEndpoint( 3657): IOException : Permission denied
08-22 16:36:13.359 W/System.err( 3657): java.net.SocketException: Permission denied
08-22 16:36:13.369 W/System.err( 3657): at org.apache.harmony.luni.platform.OSNetworkSystem.send(Native Method)
08-22 16:36:13.369 W/System.err( 3657): at dalvik.system.BlockGuard$WrappedNetworkSystem.send(BlockGuard.java:308)
08-22 16:36:13.369 W/System.err( 3657): at org.apache.harmony.nio.internal.DatagramChannelImpl.send(DatagramChannelImpl.java:359)
08-22 16:36:13.369 W/System.err( 3657): at zexing.udptester.UDPEndpoint.broadcast(UDPEndpoint.java:151)
08-22 16:36:13.369 W/System.err( 3657): at zexing.udptester.MainActivity.onClickBroadcast(MainActivity.java:162)
08-22 16:36:13.369 W/System.err( 3657): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:36:13.369 W/System.err( 3657): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 16:36:13.369 W/System.err( 3657): at android.view.View$1.onClick(View.java:2155)
08-22 16:36:13.369 W/System.err( 3657): at android.view.View.performClick(View.java:2501)
08-22 16:36:13.369 W/System.err( 3657): at android.view.View$PerformClick.run(View.java:9107)
08-22 16:36:13.369 W/System.err( 3657): at android.os.Handler.handleCallback(Handler.java:587)
08-22 16:36:13.369 W/System.err( 3657): at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 16:36:13.369 W/System.err( 3657): at android.os.Looper.loop(Looper.java:130)
08-22 16:36:13.369 W/System.err( 3657): at android.app.ActivityThread.main(ActivityThread.java:3835)
08-22 16:36:13.369 W/System.err( 3657): at java.lang.reflect.Method.invokeNative(Native Method)
08-22 16:36:13.369 W/System.err( 3657): at java.lang.reflect.Method.invoke(Method.java:507)
08-22 16:36:13.369 W/System.err( 3657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
08-22 16:36:13.369 W/System.err( 3657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
08-22 16:36:13.369 W/System.err( 3657): at dalvik.system.NativeStart.main(Native Method)
在AndroidManifest.xml中,我已经添加了这些权限。
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
另外,我在广播前获得了多播锁
//创建多播锁@ Activity.onCreate
mcastLock = wifiManager.createMulticastLock("zexing.udptester.Multicast");
//在广播期间获取多播锁
mcastLock.acquire();
if (mcastLock.isHeld()) {
Log.i(TAG, "held multicast lock");
}
udpEndpoint.broadcast(msg, port);
Log.v(TAG, "broadcast " + msg + " to port : " + port);
Log.d(TAG, "Release wifi Multicast lock to enable wifi UDP broadcast");
mcastLock.release();
// UdpEndpoint.broadcast
public void broadcast(String msg, int port) {
try {
if (broadcastChannel == null) {
Log.v(TAG, "opening write DatagramChannel");
broadcastChannel = DatagramChannel.open();
Log.v(TAG, "config write channel to non-blocking");
broadcastChannel.configureBlocking(false);
Log.v(TAG, "reuse address");
broadcastChannel.socket().setReuseAddress(true);
Log.v(TAG, "set broadcast to true");
broadcastChannel.socket().setReuseAddress(true);
Log.v(TAG, "register broadcast Channel");
broadcastChannel.register(selector, SelectionKey.OP_READ);
Log.v(TAG, "register broadcast channel successfully");
}
if (msg.length() == 0) {
Log.w(TAG, "message to broadcast is null, skip sending");
}
SocketAddress addr = new InetSocketAddress(InetAddress.getByName("255.255.255.255"), port);
Log.v(TAG, "allocate " + msg.length() + " bytes. msg.getBytes().length = " + msg.getBytes().length);
ByteBuffer bb = ByteBuffer.allocate(msg.length());
bb.clear();
bb.put(msg.getBytes());
bb.flip();
Log.v(TAG, "write channel broadcast " + msg + " to " + addr);
int bytes = broadcastChannel.send(bb, addr);
Log.v(TAG, "broadcast " + bytes + " bytes to " + addr);
} catch (UnknownHostException uhe) {
Log.e(TAG, "UnknownHostException : " + uhe.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException : " + e.getMessage());
e.printStackTrace();
}
}
答案 0 :(得分:0)
尝试向子网的广播地址发送广播示例:192.168.1.255而不是255.255 ....请确保您的端口大于1024或只是端口0,系统将为您选择一个