9

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...

Community
  • 1
  • 1
sknt
  • 963
  • 6
  • 16
  • Does your app have the `INTERNET` permission? – CommonsWare Sep 23 '15 at 22:14
  • 1
    try I think it is case sensitive. – Daniel Bo Sep 23 '15 at 22:21
  • Thanks Daniel Bo! This was the problem. (Android Studio, you could have told me that, really :/ ) Can you post this as an answer so i can accept it? – sknt Sep 23 '15 at 22:33
  • 'The code is located in the onCreate method'. That will not work. Such code should be executed in a thread or AsyncTask. – greenapps Sep 24 '15 at 07:13
  • 'ServerSocket serverSocket = new ServerSocket(4117);'. That's better. Do not bind it to the wifi address of your Android server. Why would you? – greenapps Sep 24 '15 at 07:16
  • Yes i know that accept() will be a blocking method, but as i wrote, i was trying to reduce the code to a minimum, because i had no idea what was going wrong. Binding to the wifi address was just a desperate try to get it working. But still thank you for the tipps. – sknt Sep 24 '15 at 10:38

1 Answers1

7

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
sknt
  • 963
  • 6
  • 16