Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ServerSocket permission denied

Tags:

java

android

I'm trying to setup a TCP connection between my Android phone and my Raspberry Pi. Should be easy, turns out it isn't ? So i started writing a simple server with ServerSocket. It didn't work so i reduced the code until i got this piece which is not working:

    try
    {
        String wifiip = wifiIpAddress(this);
        Log.v("SERVER", wifiip);

        ServerSocket serverSocket = new ServerSocket();
        serverSocket.setReuseAddress(true);
        serverSocket.bind(new InetSocketAddress(wifiip, 4117));
    }catch (Exception ex)
    {
        ex.printStackTrace();
    }

(The code is located in the onCreate method in my MainActivity which was created by AndroidStudio)

I also tried to create the ServerSocket with this method with no success.

ServerSocket serverSocket = new ServerSocket(4117);

The method wifiIpAddress was taken from here: https://stackoverflow.com/a/18638588/2394967 and works just fine.

The AndroidManifest.xml

<manifest ...>
<uses-sdk android:targetSdkVersion="21"></uses-sdk>
<uses-permission android:name="ANDROID.PERMISSION.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
...
</manifest>

Maybe useful informations:

  • I'm running AndroidStudio on ArchLinux
  • I tried using a new, untouched Project
  • I tried different Ports (above 1024)
  • I'm running the App on a real device (Xperia Z, Android 5.0.2)
  • I tried running the ServerSocket in another thread

Logcat

    java.net.SocketException: socket failed: EACCES (Permission denied)
W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:632)
W/System.err﹕ at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
W/System.err﹕ at java.net.PlainServerSocketImpl.create(PlainServerSocketImpl.java:38)
W/System.err﹕ at java.net.ServerSocket.<init>(ServerSocket.java:64)
W/System.err﹕ at eu.therealskynet.socketstest.MainActivity.onBtSendClicked(MainActivity.java:47)
W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err﹕ at android.view.View$1.onClick(View.java:4078)
W/System.err﹕ at android.view.View.performClick(View.java:4832)
W/System.err﹕ at android.view.View$PerformClick.run(View.java:19839)
W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err﹕ at android.os.Looper.loop(Looper.java:211)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5333)
W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
W/System.err﹕ Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
W/System.err﹕ at libcore.io.Posix.socket(Native Method)
W/System.err﹕ at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
W/System.err﹕ at libcore.io.IoBridge.socket(IoBridge.java:617)
W/System.err﹕ ... 17 more

I don't even know what "socket failed: EACCES (Permission denied)" is supposed to tell me...

like image 723
sknt Avatar asked Sep 23 '15 22:09

sknt


1 Answers

I'll just post the answer myself.

The problem was that this is case sensitive:

ANDROID.PERMISSION.INTERNET

And has to be written like this:

android.permission.INTERNET
like image 106
sknt Avatar answered Nov 15 '22 22:11

sknt