简体   繁体   English

使用C ++应用在Android下监听套接字

[英]Listening on socket under Android with C++ app

I would need to create C++ console application that runs under non-privileged user under Android 5 (rooted). 我将需要创建在Android 5(root用户)下以非特权用户身份运行的C ++控制台应用程序。 This application listens on some socket port and accepts connections. 该应用程序侦听某些套接字端口并接受连接。 It is implemented with boost::asio . 它通过boost::asio When application runs as a root, then everything works. 当应用程序作为根运行时,一切正常。 When it runs as an ordinary user, then application crashes when it tries to open acceptor: 当它以普通用户身份运行时,应用程序在尝试打开接受器时崩溃:

    const boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 3784);
    boost::asio::ip::tcp::acceptor acceptor(ioService);
    acceptor.open(endpoint.protocol()); // Here it throws an exception

The exception is a bit confusing but probably it means that I don't have access rights to touch sockets: 异常有点令人困惑,但可能意味着我没有触摸套接字的访问权限:

terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what():  open: Permission denied
Aborted

I tried to run application under user shell and under user media . 我试图在用户shell程序和用户media下运行应用程序。 I also tried to run it under user u0_a83 , that is assigned to ProxyDroid on my device. 我还尝试在分配给我设备上的ProxyDroid用户u0_a83下运行它。

I changed /system/etc/permissions/platform.xml -- I assigned permission android.permission.INTERNET to user shell and media . 我更改了/system/etc/permissions/platform.xml我为用户shellmedia分配了android.permission.INTERNET权限。 I also tried to add group media to android.permission.INTERNET , but nothing has helped. 我还尝试将组媒体添加到android.permission.INTERNET ,但是没有任何帮助。

Btw. 顺便说一句。 this is a special case when I really need to implement application in C++ to run under Android. 当我确实需要在C ++中实现应用程序以在Android下运行时,这是一个特例。 Implementation in Java is not acceptable in my project. 在我的项目中,Java的实现是不可接受的。

It seems that the best solution is to use users that are predefined in Android and corresponds to capabilities available on the system: https://android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config.h . 似乎最好的解决方案是使用在Android中预定义并与系统上可用功能相对应的用户: https//android.googlesource.com/platform/system/core/+/master/include/private/android_filesystem_config。 ^ h In my case I can run the app under user inet (3003) that can create sockets. 就我而言,我可以在可以创建套接字的用户inet(3003)下运行该应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM