Android browser can't connect to local nanohttpd service

961 views Asked by At

I'm creating a service as root using WakefulBroadcastReceiver approach for some test purpose (Note that I just tested with an Activity launching the server, same thing happen):

MyService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Server starting");

    // Create the server
    server = new WebServer(6666);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                server.start();
            } catch (IOException e) {
                Log.d(TAG, e.getMessage());
            }
        }
    }).start();
    ...
    return START_STICKY;
}

WebServer.java :

public class WebServer extends NanoHTTPD {
  @Override
  public Response serve(IHTTPSession session) {
    Log.d(TAG, "Serving");

    return new NanoHTTPD.Response("<html><body>test</body></html>");
}

I push using adb the APK in /system/app so the service can be called without using an activity first. Then restart the shell.

I can see logs of the server waiting for connection and then :

  • if I use wget http://127.0.0.1:6666 using adb shell I can see the log "Serving".
  • if I use the device browser with the same address (tried localhost too), the server isn't reached.

I use API18 and default Nexus7 AVD (tried with Genymotion too and a samsung tablet) but nothing works. There is no proxy configured in network settings.

I don't get why browser (on the device) isn't reaching my 6666 port on 127.0.0.1 (on the device too)

Here is the manifest :

<application android:allowBackup="true" android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">

        <service android:name=".MyService" android:label="Tool" android:enabled="true"/>

        <receiver android:name=".receiver.BootCompleteReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
    </application>

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
1

There are 1 answers

0
Michael Laffargue On BEST ANSWER

I was running crazy and finally someone sent me this link : https://superuser.com/questions/188058/which-ports-are-considered-unsafe-on-chrome

Port 6666 is considered unsafe ... I wish it would be written on the page instead of couldn't reach server.