flutterdartserver

How to connect to the localhost of an Android emulator?


Note that I'm not trying to connect to the localhost of the host machine from the Android emulator, I'm pretty much doing it in reverse - I started a server on the localhost inside Android emulator and want to connect to that localhost from the computer's browser.

I'm using Angel framework on Flutter.

import 'package:angel_framework/angel_framework.dart';
import 'package:angel_framework/http.dart';

Future<AngelHttp> startWebServer({int port = 3000}) async {
  var app = Angel();
  var http = AngelHttp(app);

  await http.startServer('localhost', port);
  print('Started HTTP server at ${http.server.address}:${http.server.port}');

  app.get('/', (req, res) => res.write('<p>Hello, world!<p>'));

  return http;
}

What settings should I put and what address should I use in my browser to receive 'Hello World' from the server running on the emulator?


Solution

  • You can do that with adb forward. See port forwarding

    adb forward tcp:3000 tcp:3000
    

    Then on your computer you can connect to http://localhost:3000 and it will be routed to port 3000 of the emulator.