SocketException:EACCESS(权限被拒绝)

时间:2015-05-08 07:10:10

标签: java android sockets permissions

我正在尝试在Android中创建一个套接字,但我继续获得EACCESS(Permission denied)错误。

以下是来自UI线程的单独线程中的代码。

public class Client implements Runnable {
    private final static String SERVER_ADDRESS = "173.246.12.125";

    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_ADDRESS);
            DatagramSocket socket = new DatagramSocket();
            ...

我的Android权限包括:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />

并且仅仅因为一些stackoverflow答案,我也添加了这个:

    <permission android:name="android.permission.INTERNET"/>

但我仍然无法创建套接字。

我错过了什么? 错误输出:

 java.net.SocketException: socket failed: EACCES (Permission denied)
        at libcore.io.IoBridge.socket(IoBridge.java:623)
        at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:93)
        at java.net.DatagramSocket.createSocket(DatagramSocket.java:157)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:65)
        ...

Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
        at libcore.io.Posix.socket(Native Method)
        at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
        at libcore.io.IoBridge.socket(IoBridge.java:608) 
        at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:93)
        at java.net.DatagramSocket.createSocket(DatagramSocket.java:157)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
        at java.net.DatagramSocket.<init>(DatagramSocket.java:65)
        at com.austinfathman.traffic_server.Client.run(UDP.java:73)
        at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:-3)

我遇到了类似的错误。事实证明,如果您尝试绑定到正在使用的端口,它将无法正常工作。

DatagramSocket socket = new DatagramSocket(3996);

这对我有用。