简体   繁体   English

Android java.net.SocketException:套接字失败:EACCES(权限被拒绝)

[英]Android java.net.SocketException: socket failed: EACCES (Permission denied)

I'm trying to create a TCP/IP connection with a device that is running embedded linux, I'm using from android the USB tethering to have a ethernet connection between the Android device and the Embedded Linux device, I modified the embedded linux device to support RNDIS device driver. 我正在尝试使用运行嵌入式linux的设备创建TCP / IP连接,我正在使用Android的USB绑定在Android设备和嵌入式Linux设备之间建立以太网连接,我修改了嵌入式linux设备支持RNDIS设备驱动程序。

The Android and the embedded linux device are working correctly, a ethernet conection was established succesfully, I can ping from embedded linux to android device and viceversa. Android和嵌入式Linux设备工作正常,以太网连接成功建立,我可以从嵌入式Linux ping到android设备,反之亦然。

On my android application I created a simple service to establish a TCP/IP connection with the embedded linux device, the problem happens when I try to connect i got the following error: 在我的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

I have configured my manifest as follows: 我已将配置文件配置如下:

<?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>

The most extrange thing is if I use a Android VM (Genymotion) I'm able to connect and everithing works correctly, but when I try to use it on a real device I get the error. 最重要的是如果我使用Android VM(Genymotion)我能够连接并且正常工作,但是当我尝试在真实设备上使用它时,我得到了错误。

Thank you in advance for any comment. 提前感谢您的任何评论。

EDIT: 编辑:

This is the code I'm using to create the binding, I'm using the port 6540, and the IP is 192.168.42.130 这是我用来创建绑定的代码,我使用的是端口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();
    }

}       

I believe that when I try to create the socket the error occurs ( Connection = new Socket(serverAddr, SERVERPORT); ). 我相信当我尝试创建套接字时会发生错误( Connection = new Socket(serverAddr,SERVERPORT); )。

android:isolatedProcess 机器人:isolatedProcess

If set to true, this service will run under a special process that is isolated from the rest of the system and has no permissions of its own. 如果设置为true,则此服务将在与系统其余部分隔离的特殊进程下运行,并且没有自己的权限。 The only communication with it is through the Service API (binding and starting). 与它的唯一通信是通过Service API(绑定和启动)。

我找到了一个解决这个问题的方法,我将TCP客户端从服务更改为主应用程序内的异步任务,我不明白为什么它在Genymotion模拟器上工作但它在真实设备上根本没用使用服务时。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

暂无
暂无

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

相关问题 Android java.net.SocketException:socket failed: EACCES (Permission denied) - Android java.net.SocketException:socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:Android Studio 中的 EACCES(权限被拒绝) - java.net.SocketException: socket failed: EACCES (Permission denied) in Android Studio W/System.err: java.net.SocketException: socket failed: android 中的 EACCES(权限被拒绝) - W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied) in android java.net.SocketException:套接字失败:EACCES(权限被拒绝)不能通过uses-permission解决 - java.net.SocketException: socket failed: EACCES (Permission denied) not solved by uses-permission Android java.net.SocketException:套接字失败:通过Web服务将值插入SQL Server时,EACCES(权限被拒绝) - Android java.net.SocketException: socket failed: EACCES (Permission denied) while inserting value into sql server through Web service 将信息发送到服务器java.net.SocketException:套接字失败:EACCES(权限被拒绝) - Sending information to server, java.net.SocketException: socket failed: EACCES (Permission denied) createServerSocket返回java.net.SocketException:套接字失败:EACCES(权限被拒绝) - createServerSocket returning java.net.SocketException: socket failed: EACCES (Permission denied) java.net.SocketException:套接字失败:EACCES(权限被拒绝)MongoDB连接 - java.net.SocketException: socket failed: EACCES (Permission denied) MongoDB connection 错误消息“java.net.SocketException:套接字失败:EACCES(权限被拒绝)” - Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM