Create a socket in android code(not in android application) getting Permission Denied

巧了我就是萌 提交于 2019-12-21 21:29:09

问题


I am trying to open a socket in the android source code. Specifically, right now, I am in the DisplayDevice.cpp file, but the location of the socket code may change. Right now after I do:

int fd = socket(AF_INET, SOCK_STREAM, 0);

fd gets returned as -1, and when i check the error message it is listed as Permission Denied. I have looked around a lot for this, most answers involve adding the internet permission to the AndroidManifest file. This will not work for me as the code I am adding is inside of the android source code.

I was wondering if there is a way to bypass the permission denied. Or if there is a better way to do this/ a different type of socket to use(right now I am using sockets from

Thank you.


回答1:


It is quite natural that you get Permission Denied error. This is simply because you don't have correct permission :). Check out android permission model!

Imagine a scenario like a normal user in an operating system and you write a program which tries to open a socket like yours. You would most probably face the same problem, depending on where the named socket is to be created.

As you are trying to create the socket in DisplayDevice.cpp (compiling android from the source), you may be interested in compiling the source as a superuser. Here is a solution posted by m-ric (I have never tested it).

Some useful pointers/references in similar direction and which I found useful during research on this enthralling topic are:

  1. https://android.stackexchange.com/questions/18857/how-to-build-compile-su-from-source
  2. execv command => http://code.google.com/p/superuser/source/browse/trunk/su/su.c?r=2#169
  3. https://github.com/ChainsDD/su-binary
  4. http://e2e.ti.com/support/omap/f/849/p/178679/648158.aspx#648158
  5. A video from Google I/O 2011 http://www.youtube.com/watch?v=5yorhsSPFG4


来源:https://stackoverflow.com/questions/15302363/create-a-socket-in-android-codenot-in-android-application-getting-permission-d

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