我想在Android中使用套接字。但它失败了,因为我得到一个EACCESS异常。我包含了超过100个Android许可,只是为了确保,这不是一个许可问题。,)如果我接受这些权限,我也会被问到是否手动安装它…
2185 - 2199/com.myexample。wakeonlan W/系统。error: Caused by: libcore.io.ErrnoException: bind failed: EACCES (Permission denied)
2185 - 2199/com.myexample。wakeonlan W/系统。错误:在libcore.io.Posix。bind(本机方法)
2185 - 2199/com.myexample。wakeonlan W/系统。错误:at libcore.io.ForwardingOs.bind(ForwardingOs.java:39)
2185 - 2199/com.myexample。wakeonlan W/系统。错误:at libcore.io. ibridge .bind(ibridge .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,它必须已经被我的设备上的另一个程序使用。