Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write dead simple native SERVER program (not app) for Android devices?

First of all, I believe, it's not a duplicate question. I don't want to write a native app with NDK, I just want to use Android device as a cheap Linux server. Server, in this context, means a program, which has no UI. Also, the server is written in bare C++, but uses no libraries at all, so that's why should that question differ from others.

The story

We've written a small server for Linux, it has a webserver interface, so you connect to it with a vanilla browser, and you can play with it. We've compiled it to Linux/x86, Linux/ARM (GuruPlug), Linux/PPC (some kind of NAS), and Mac OS X Darwin platforms.

The Linux/ARM version also runs well on my Palm Pré smartphone. I've just copied the executable onto my phone (in webOS scene, there's no such thing like jailbreak, there's an official "developer mode", which if you activate by typing a secret key combination, you can log in to your phone with ssh or use local ssh app). So, our small server program (daemon, may say) is running on even the smallest Linux devices. (It's funny, when connect with your giga-powered desktop machine's browser to a smartphone server.)

The server program is designed to run on even the smallest Linux (as I said, it runs on NAS, router, smartphone), it requires only STDLIB and PTHREADS.

The question

What't the straight way to get that simple server program run on an Android device?

  • We have no Android device yet. But I assume, any ARM-based one will do. The server's memory footprint is so small, that it will be no problem.
  • I assume, the device must be rooted to copy executable to it and run.
  • I don't want to install a whole new Linux on the device. It should keep running Android, while our server works in the background. The Linux distribution which enough for Dalvik, is enough for our server, I'm pretty sure.
  • The server should use no external things (USB, touchscreen, audio, camera, mic - thanks, not) only a port for webserver (e.g. 8080), which is not allocated by Android.
  • I have ARM GCC toolchain, the installation of it is not the part of the question (except if there're tricks).
  • The installation of the program should be manual (scp, whatever). Also, I don't want to make a package or any automated deployment.
  • The problem of the launching of the server program is another question, we'd ignore now.

So, I want just do the same thing with an Android device (phone, tablet), just as I did on my webOS device: copy the server program and run on it. Computers are for that, running programs, aren't they? ;)

like image 565
ern0 Avatar asked Nov 03 '11 12:11

ern0


2 Answers

I have not really tried it, but it should be possible to use the ARM GCC compiler you have, or the one provided by the Android NDK to compile native application, the same as you would use it on your desktop.

If your application is small (not too many files), you can manage the compilation by hand (or in a simple Makefile). The following commands are from the LuaJIT compilation guide for Android:

/tmp$ cat test.c
#include <stdio.h>
int main() {
        printf("Hello world!\n");
        return 0;
}
/tmp$ NDK=/opt/ndk
/tmp$ NDKABI=8
/tmp$ NDKVER=$NDK/toolchains/arm-linux-androideabi-4.4.3
/tmp$ NDKP=$NDKVER/prebuilt/darwin-x86/bin/arm-linux-androideabi-
/tmp$ NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
/tmp$ ${NDKP}gcc $NDKF -o test test.c 
/tmp$ file test
test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

Obviously, use linux-x86 instead of darwin-x86 if you are on Linux (not Mac OS X, also not sure about Windows), and change the path to NDK to where you installed it.

Now you have to move the compiled binary somewhere to your Android device, and run adb shell to see if it works:

/tmp$ ADB=/opt/sdk/platform-tools/adb
/tmp$ $ADB push test /data/
332 KB/s (3343 bytes in 0.009s)
/tmp$ $ADB shell
# /data/test
Hello world!
like image 165
Michal Kottman Avatar answered Oct 10 '22 02:10

Michal Kottman


You definitely need a rooted android device. I recommend that you install busybox to get a bunch of nice command line tools such as wget, tar, gzip and many more. You can get shell access to your device by using adb shell and then su to get root access. You can either use adb push or wget to put your server on the device.
The /system partition is mounted as read only and depending on the manufacturer there are a lot of protections that restrict you from writing there so you should copy your server somewhere on the /data partition (e.g. mkdir /data/myserver). After this, everything should be pretty straight forward.

like image 28
tbg Avatar answered Oct 10 '22 04:10

tbg