On a rooted Android device, how can I open a port < 1024?
I am trying to open port 123 on a rooted Galaxy Tab running 2.3.3. The app starts up with Super User permissions and has INTERNET permissions. But, I get the following exception:
E/xxxxxxxxxxxxxxxxxxxxxxxxx.AndroidServerActivity( 4809): Unable to startup the NTP Server..., java.net.BindException: Permission denied
W/System.err( 4809): java.net.BindException: Permission denied
W/System.err( 4809): at org.apache.harmony.luni.platform.开发者_如何学编程OSNetworkSystem.bind(Native Method)
W/System.err( 4809): at dalvik.system.BlockGuard$WrappedNetworkSystem.bind(BlockGuard.java:268)
W/System.err( 4809): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:81)
W/System.err( 4809): at java.net.DatagramSocket.createSocket(DatagramSocket.java:193)
W/System.err( 4809): at java.net.DatagramSocket.<init>(DatagramSocket.java:77)
W/System.err( 4809): at net.sf.atomicdate.NTPClient.<init>(NTPClient.java:79)
W/System.err( 4809): at net.sf.atomicdate.NTPClient.<init>(NTPClient.java:66)
W/System.err( 4809): at xxxxxxxxxxxxxxxxxxxxxxxxxActivity.onCreate(AndroidServerActivity.java:141)
W/System.err( 4809): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err( 4809): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
W/System.err( 4809): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
W/System.err( 4809): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
W/System.err( 4809): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
W/System.err( 4809): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err( 4809): at android.os.Looper.loop(Looper.java:123)
W/System.err( 4809): at android.app.ActivityThread.main(ActivityThread.java:3687)
W/System.err( 4809): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err( 4809): at java.lang.reflect.Method.invoke(Method.java:507)
W/System.err( 4809): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
W/System.err( 4809): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
W/System.err( 4809): at dalvik.system.NativeStart.main(Native Method)
I've done some searching and I'm not finding the correct way to do this. Anyone have any tips or examples?
I believe your assumption that the app is "starting up with Super User permissions" is flawed. The app itself is running inside the Dalvik VM, which implies that it cannot possess superuser permissions. Whatever application you launch using the "su" command, can possess root permissions, and therefore can open ports <1024. But the Android application itself cannot. See AOSP issue 4039 for discussion on the matter, and to place your vote for the Dalvik VM to gain this support.
If all you want to do is start an NTP server, you may be better off packaging ntpd with your application, and launch it via su
. Alternatively, you could try starting your Java NTP server on a non-standard port, and use a command-line utility (e.g., netcat) to "proxy" the protected port 123 into your non-standard (>=1024) port.. Something like: nc -l -u localhost 123 | nc -u localhost 1123
(Wikipedia)
As it turns out, ntpd is actually available on the device. So for my purposes, I just started the native ntpd server:
ntpd -l 123
And it worked as I needed it too. I will have to experiment with non-rooted devices and work on the redirect method above to do the same thing.
精彩评论