I wrote an Android application that acts as HTTP server by opening a socket and listening on port 8080. During development I used the QEMU-based Android emulator that is integrated with Android Studio. Simply sending requests to the emulated Android machine does not work, some preparation is necessary.
I used the post Android AVD Networking by rhill solutions as basis, which unfortunately only exists on the Internet Archive in 2022.
Setup
I had to setup port forwarding from my local machine to the Android emulator (AVD, Android Virtual Device). The first emulator has a management interface on port 5554, which you can connect to with telnet:
$ telnet localhost 5554 Connected to localhost. Escape character is '^]'. Android Console: Authentication required Android Console: type 'auth <auth_token>' to authenticate Android Console: you can find your <auth_token> in '/home/cweiske/.emulator_console_auth_token' OK auth thisismysecretaccesskey Android Console: type 'help' for a list of commands OK redir add tcp:8080:8080 OK quit Connection closed by foreign host.
This enabled the forwarding of requests to localhost:8080 to the Android application listening on port 8080 in the emulator:
$ curl -i localhost:8080/ HTTP/1.1 200 OK Content-Type: text/html Date: Wed, 20 Jul 2022 20:16:04 GMT Connection: keep-alive Content-Length: 1166 <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>louyapi</title> </head> <body> <h1>louyapi - local OUYA API server</h1>