安卓java.net.SocketException : socket failed: EACCES (Permission denied)

标签 android linux tcp embedded ethernet

我正在尝试与运行嵌入式 linux 的设备创建 TCP/IP 连接,我使用 android 的 USB 网络共享在 Android 设备和嵌入式 Linux 设备之间建立以太网连接,我修改了嵌入式 linux 设备支持 RNDIS 设备驱动程序。

Android 和嵌入式 linux 设备工作正常,以太网连接已成功建立,我可以从嵌入式 linux ping 到 android 设备,反之亦然。

在我的 android 应用程序上,我创建了一个简单的服务来与嵌入式 linux 设备建立 TCP/IP 连接,当我尝试连接时出现问题,我收到以下错误:

02-17 13:53:13.300: E/TCP Client(2576): C: Connecting...
02-17 13:53:13.310: E/TCP Client(2576): C: Error
02-17 13:53:13.310: E/TCP Client(2576): java.net.SocketException: socket failed: EACCES (Permission denied)
02-17 13:53:13.310: E/TCP Client(2576):     at libcore.io.IoBridge.socket(IoBridge.java:583)
02-17 13:53:13.310: E/TCP Client(2576):     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:201)
02-17 13:53:13.310: E/TCP Client(2576):     at java.net.Socket.startupSocket(Socket.java:559)
02-17 13:53:13.310: E/TCP Client(2576):     at java.net.Socket.<init>(Socket.java:225)
02-17 13:53:13.310: E/TCP Client(2576):     at com.example.service.TCPClientThread.run(TCPClientThread.java:75)
02-17 13:53:13.310: E/TCP Client(2576): Caused by: libcore.io.ErrnoException: socket failed: EACCES (Permission denied)
02-17 13:53:13.310: E/TCP Client(2576):     at libcore.io.Posix.socket(Native Method)
02-17 13:53:13.310: E/TCP Client(2576):     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:181)
02-17 13:53:13.310: E/TCP Client(2576):     at libcore.io.IoBridge.socket(IoBridge.java:568)
02-17 13:53:13.310: E/TCP Client(2576):     ... 4 more

我的 list 配置如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dea600"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/example"
        android:label="@string/appName" >
        <activity
            android:name="com.example.gui.mainActivity"
            android:configChanges="orientation|screenSize"
            android:screenOrientation="landscape"
            android:theme="@android:style/Theme.Holo.Light.DarkActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <service
            android:name="com.example.service.TCPClientService"
            android:isolatedProcess="true"
            android:label="TCP Client Service"
            android:process=":TCPClientService" >
        </service>

        <receiver
            android:name="com.example.service.TCPStartClientService"
            android:enabled="true"
            android:exported="true" 
            android:process=":StartTCPService">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

     </application>

</manifest>

最奇怪的是,如果我使用 Android VM (Genymotion),我可以连接并且一切正常,但是当我尝试在真实设备上使用它时,我会收到错误消息。

提前感谢您的任何评论。

编辑:

这是我用来创建绑定(bind)的代码,我使用端口 6540,IP 是 192.168.42.130

public void run() {           
    try {             
        //here you must put your computer's IP address.             
        InetAddress serverAddr = InetAddress.getByName(SERVERIP);               
        Log.e("TCP Client", "C: Connecting...");
        //create a socket to make the connection with the server             
        Connection = new Socket(serverAddr, SERVERPORT);
        Log.e("TCP Client", "C: Connected...");
        // Output Stream
        OutStream = new DataOutputStream((OutputStream)Connection.getOutputStream());
        // Input stream
        InStream = new DataInputStream((InputStream)Connection.getInputStream());

        while (ThreadStatus) {
            try {

                String message = myReadStream(InStream);
                if (message.length() > 0) {
                    Log.e("TCP Client", "Received message ... " + message);
                    if (mMessageListener != null){
                        mMessageListener.messageReceived(message);
                    }
                }
            } catch (Exception e) {      
                ThreadStatus = false;
                Log.e("TCP Client", "S: Error", e);               
            }
        }

    } catch (Exception e) {               
        Log.e("TCP Client", "C: Error", e);           
    }  finally {                 
        try {
            Connection.close();
            OutStream.close();
            InStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (mConnectionEndListener != null){
        mConnectionEndListener.notifyConnectionEnd();
    }

}       

我相信当我尝试创建套接字时会发生错误 (Connection = new Socket(serverAddr, SERVERPORT);)。

最佳答案

android:isolatedProcess

如果设置为 true,此服务将在与系统其余部分隔离的特殊进程下运行,并且没有自己的权限。与它的唯一通信是通过服务 API(绑定(bind)和启动)。

关于安卓java.net.SocketException : socket failed: EACCES (Permission denied),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21838640/

相关文章:

android - 使用自定义流布局时,点击事件不会在所有设备上传播

linux - 如何在 Linux 中将天数转换为秒数

linux - shell:使用sed替换文件中的字符串

c - 如何使用轮询接受多个客户端? (TCP 服务器) (C)

android - 访问 MediaPlayer 中的音频缓冲区

java - 如何将点击播放/暂停添加到 VideoView?

Linux/Unix Socket自连接

c - 如何检测 TCP 套接字断开连接(使用 C Berkeley 套接字)

java - toString 方法返回随机字符

c++ - 尝试从方法返回指向对象的指针时出现段错误