简体   繁体   English

Android java.net.SocketException:socket failed: EACCES (Permission denied)

[英]Android java.net.SocketException:socket failed: EACCES (Permission denied)

I am getting an Android app, but when I launch it, I get an error in my console.我正在获取一个 Android 应用程序,但是当我启动它时,我的控制台出现错误。 I am using a Datagram socket to create a connection and I'm using 2 classes: MainActivity (it's the main activity of the app) and UdpClientServer to create the connection.我正在使用数据报套接字来创建连接,并且我正在使用 2 个类: MainActivity (它是应用程序的主要活动)和UdpClientServer来创建连接。

Here the code MainActivity:这里的代码 MainActivity:

public class MainActivity extends Activity {

private UdpClientServer cu;
private EditText textIpScheda;
private EditText textUdpPort;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textIpScheda = (EditText) findViewById(R.id.textIpScheda);
    textUdpPort = (EditText) findViewById(R.id.textUdpPort);


    try {
        cu = new UdpClientServer(this);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public EditText getTextIpScheda(){
    return textIpScheda;
}

public void setTextIpScheda(EditText textIpScheda){
    this.textIpScheda = textIpScheda;
}

public EditText getTextUdpPort() {
    return textUdpPort;
}

public void setTextUdpPort(EditText textUdpPort) {
    this.textUdpPort = textUdpPort;
}

Here the code UdpClientServer:这里的代码 UdpClientServer:

public class UdpClientServer {

public static String sReceive;
private static DatagramSocket dSocket;
int receiveBufferSize = 1024;
int portUdp = 0;
final String PINGACMD = "AT*PINGA001";
InetAddress ipScheda;

byte[] receiveData = new byte[receiveBufferSize];
private MainActivity gui = null;

public UdpClientServer(MainActivity gui) throws SocketException, IOException {
    this.gui = gui;

    portUdp = Integer.parseInt(String.valueOf(gui.getTextUdpPort().getText()));
    dSocket = new DatagramSocket(portUdp);
}

public void run(){
    while (true) {
        // svuotamento buffer
        Arrays.fill(receiveData, (byte) 0);
        DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);

        try {
            dSocket.receive(receivePacket);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ipScheda = receivePacket.getAddress();
        int port = receivePacket.getPort();
        gui.getTextUdpPort().setText("" + port);
        gui.getTextIpScheda().setText(ipScheda.getHostAddress());

        sReceive = new String(receivePacket.getData());
        this.sendCommand(PINGACMD);
    }

}

public void sendCommand(String outSentence){

    byte[] sendData = outSentence.getBytes();

    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipScheda, portUdp);

    try {
        dSocket.send(sendPacket);
        Thread.sleep(100);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    finally {
    }
}

} }

And here the logcat:这里是 logcat:

    12-29 11:43:22.291  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ java.net.SocketException: socket failed: EACCES (Permission denied)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:623)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:93)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at java.net.DatagramSocket.createSocket(DatagramSocket.java:157)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at java.net.DatagramSocket.<init>(DatagramSocket.java:80)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.example.matteo.myfirstapp.UdpClientServer.<init>(UdpClientServer.java:32)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.example.matteo.myfirstapp.MainActivity.onCreate(MainActivity.java:24)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5933)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.Posix.socket(Native Method)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:608)
12-29 11:43:22.294  28914-28914/com.example.matteo.myfirstapp W/System.err﹕ ... 18 more

In my AndroidManifest.xml just added string在我的 AndroidManifest.xml 中刚刚添加了字符串

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

but it does not work.但它不起作用。

Try adding the following:尝试添加以下内容:

<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.ACCESS_NETWORK_STATE" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android java.net.SocketException:套接字失败:EACCES(权限被拒绝) - Android java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:Android Studio 中的 EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied) in Android Studio W/System.err: java.net.SocketException: socket failed: android 中的 EACCES(权限被拒绝) - W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied) in android java.net.SocketException:套接字失败:EACCES(权限被拒绝)不能通过uses-permission解决 - java.net.SocketException: socket failed: EACCES (Permission denied) not solved by uses-permission Android java.net.SocketException:套接字失败:通过Web服务将值插入SQL Server时,EACCES(权限被拒绝) - Android java.net.SocketException: socket failed: EACCES (Permission denied) while inserting value into sql server through Web service createServerSocket返回java.net.SocketException:套接字失败:EACCES(权限被拒绝) - createServerSocket returning java.net.SocketException: socket failed: EACCES (Permission denied) 将信息发送到服务器java.net.SocketException:套接字失败:EACCES(权限被拒绝) - Sending information to server, java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:EACCES(权限被拒绝)MongoDB连接 - java.net.SocketException: socket failed: EACCES (Permission denied) MongoDB connection 错误消息“java.net.SocketException:套接字失败:EACCES(权限被拒绝)” - Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM