发送的广播数据报在Android中无法通过EACCES

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发送的广播数据报在Android中无法通过EACCES相关的知识,希望对你有一定的参考价值。

我想在android中使用套接字。但是它失败了,因为我收到了EACCESS异常。为了确保这一点,我包括了100多个Android权限,这不是一个权限问题。 ;)我还会问我是否接受这些权限,是否手动安装...

2185-2199 / com.myexample.wakeonlan W / System.err:原因:libcore.io.ErrnoException:绑定失败:EACCES(权限被拒绝)

2185-2199 / com.myexample.wakeonlan W / System.err:位于libcore.io.Posix.bind(本机方法)

2185-2199 / com.myexample.wakeonlan W / System.err:位于libcore.io.ForwardingOs.bind(ForwardingOs.java:39)

2185-2199 / com.myexample.wakeonlan W / System.err:位于libcore.io.IoBridge.bind(IoBridge.java:87)

我尝试使用:

DatagramSocket socket = new DatagramSocket(1);
        try
        {
            InetSocketAddress address = new InetSocketAddress(
                    "255.255.255.255", 0);
            DatagramPacket datagram = new DatagramPacket(packetData,
                    packetLength, address);
            socket.setBroadcast(true);
            socket.send(datagram);
        }
        finally
        {
            if (socket != null)
            {
                socket.close();
            }
        }

错误:

07-25 10:07:32.462    2185-2185/com.myexample.wakeonlan D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ java.net.BindException: bind failed: EACCES (Permission denied)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at libcore.io.IoBridge.bind(IoBridge.java:89)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:68)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.net.DatagramSocket.createSocket(DatagramSocket.java:133)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.net.DatagramSocket.<init>(DatagramSocket.java:78)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at com.myexample.wakeonlan.WakeOnLan.wake(WakeOnLan.java:137)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at com.myexample.wakeonlan.WakeOnLan.wake(WakeOnLan.java:23)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at com.myexample.wakeonlan.EntryActivity$MyAsyncTask.doInBackground(EntryActivity.java:57)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at com.myexample.wakeonlan.EntryActivity$MyAsyncTask.doInBackground(EntryActivity.java:41)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ Caused by: libcore.io.ErrnoException: bind failed: EACCES (Permission denied)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at libcore.io.Posix.bind(Native Method)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ at libcore.io.IoBridge.bind(IoBridge.java:87)
07-25 10:07:42.622    2185-2199/com.myexample.wakeonlan W/System.err﹕ ... 14 more

相关代码:

 private class MyAsyncTask extends AsyncTask<String, Void, String> {

        String msg = "";
        @Override
        protected String doInBackground(String... params) {
            String adr = "";
            if(params.length > 0)
                adr = params[0];
            else
            {
                msg = "No Input for Mac Address";
                return msg;
            }

            try {
                com.example.ethernet.wakeonlan.WakeOnLan.wake(adr);

                msg = "ok" + adr;

            } catch (Exception e) {
                msg = "not ok" + adr;
                e.printStackTrace();
            }

            return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.textView);
            txt.setText(msg);
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

单击按钮:

public void sendWakeOnLan(View v)
    {
        String adr = "a valid mac address";
        new MyAsyncTask().execute(adr);
    }
答案

由于声誉我无法添加评论我认为最好更改子网广播地址示例的IP地址255.255.255.255:192.168.1.255 ...,具体取决于您的网络地址。并且也将您的端口固定为1024以外的主要值

另一答案

在我的情况下,此问题是由于我试图绑定DatagramSocket而引起的,该DatagramSocket必须已被设备上的另一个程序使用。我更改了端口,它正常工作。

以上是关于发送的广播数据报在Android中无法通过EACCES的主要内容,如果未能解决你的问题,请参考以下文章

android学习笔记 解决无法发送自定义广播的问题

Android本地广播的使用

Android基础总结

android有序广播和无序广播的区别

Android15.2 广播

Android广播阻塞、延迟问题