How to bind to a port less than 1024 in Android?

二次信任 提交于 2019-11-28 03:15:43

问题


I have an Android service trying to bind to a server socket port 24. As it is privileged port, it is failing with a bind exception. I am wondering what I need to do to get this working. I see this was asked couple of times in this forum, but without a resolution.

This service runs on a device that runs on Android. We build android platform for the device. We have all the control we need.


回答1:


To bind to a port less than 1024, you need to be root and there are two ways of doing that.

  1. System app : The app should be installed into /system/apps folder from where it shall be run as root. However, there are some signature issues for which help is available. Distribution is also a problem with system apps.

  2. su binary : The su binary can be invoked using a simple exec("su -c [command]"). But things are easier said than done. You need to run an android processes not just a single command. Hence, there is libsuperuser which provides you the methods and has a full-length doc page.




回答2:


This is a very similar question: Run secure API calls as root, android, though they are trying to run as root for a different reason.

That made me wonder about installing 'system apps', and I saw this: https://android.stackexchange.com/questions/27/rooted-how-to-install-a-system-app



来源:https://stackoverflow.com/questions/7870898/how-to-bind-to-a-port-less-than-1024-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!