Are you confused by the title? Setting up an HTTP server on an Android phone???
Are you kidding me?
Yes, we really want to set up an HTTP server on an Android phone. When it comes to HTTP servers, the first thing that usually comes to mind is Apache or nginx. Can Apache and nginx run on Android???
Of course, we can't run these servers on Android phones. This time, we are running a simple HTTP server written in Golang on Android. Because Golang can be cross-compiled, I tried selecting the Linux operating system and the arm CPU architecture, and then running it on the phone. And it actually worked, running an HTTP server on an Android phone.
Surprise!
Now, let's get back to the topic. In order to develop and write Golang, we need to set up the Golang development environment. Here is the link for Golang configuration. I developed it on Ubuntu and created a new file called service.go.
1 package main
2
3 import (
4 "net/http"
5 )
6
7 func main() {
9 http.HandleFunc("/",myResponse)
10 http.ListenAndServe("127.0.0.1:8888",nil)
11 }
12
13 func myResponse(w http.ResponseWriter,r* http.Request) {
14 w.Write([]byte("<html><center> <font size="40">hello I am go service</font></center></html>"))
15 }
For those who are not familiar with Go, please note that do not press Enter randomly to create new lines. Go is similar to Python and does not use ";" to end lines. Let me explain briefly.
In http.HandleFunc("/",myResponse)
, the first parameter is the URL for registering the HTTP service. Here we fill in "/"
, so we can access it directly with localhost:8888
. If we fill in "/test"
, the URL for accessing it will be localhost/test:8888
.
In http.ListenAndServe("127.0.0.1:8888",nil)
, the first parameter is the IP address and port to listen to, and the second parameter is set to nil
.
Now, let's compile and run it. For running the program on the local machine, we can use the default compilation parameters go build service.go
. Then run ./service
. Enter the URL in the browser and you should be able to access it.
Chrome
Next, let's port this program to Android. The Android phone must have root access. If your phone is not rooted and you don't want to root it, you can use an emulator. Just change the CPU instruction set parameter. First, let's run it on the phone. My phone is Honor 6 with a Hisilicon 920 CPU. I found that the instruction set for Hisilicon 920 is arm32. Okay, let's compile it.
Before compiling, let's rename the compiled program. Execute GOOS="linux" GOARCH="arm" go build service.go
. We will get an executable file. Use the file
command to check the file type. file service
service: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, not stripped
We can see that this file is a 32-bit ARM program. Okay, let's copy it to the root directory of the Android phone and then change the file permissions.
Screenshot_2018-04-08-19-18-11.png
Screenshot_2018-04-08-19-18-20.png
Then, run it in juiceSSH and enter the URL in the browser to check if it works.
Screenshot_2018-04-08-19-19-43.png
No issues.
Next, let's compile it for the emulator. I'm using Genymotion with the Google Nexus 5 system. I searched and found that the Nexus 5 uses the Qualcomm Snapdragon 800 CPU. Here comes the problem. Snapdragon 800 uses the arm32 instruction set. In theory, we can directly copy the program and run it, but it shows "/system/bin/sh: ./service_arm32: not executable: 32-bit ELF file"
. Then I thought about it and realized that the CPU should be the one from the computer. So let's try compiling it for X64
. Still not working. Let's try X86
. Finally, it works. It turns out that the instructions should match the CPU of the emulator.
Execute GOOS="linux" GOARCH="386" go build service.go
. Copy it to the emulator, and then follow the same steps as on the phone. Copy it to the root directory, set the permissions, and run it using adb shell
. Then, check if it can be accessed in the emulator's browser.
Genymotion
Alright, we have successfully run an HTTP server on Android.