我试图创建一个TCP/IP连接与运行嵌入式Linux的设备,我从android使用USB tethering有一个Android设备和嵌入式Linux设备之间的以太网连接,我修改了嵌入式Linux设备以支持RNDIS设备驱动程序。Android java.net.SocketException:套接字失败:EACCES(权限被拒绝)
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
我已经配置我的清单如下:
<?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)我是能够连接和everithing工作正常,但是当我尝试使用它真实的设备我得到错误。
非常感谢您的任何评论。
编辑:
这是我用来创建绑定,我使用的端口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);)。
向我们展示套接字创建和绑定代码,并指出最终在缩略堆栈跟踪中引用的代码的特定行(查看它直到它引用您的代码)。您尝试使用哪个端口号和地址? –
同样,您需要阅读完整的堆栈跟踪并确定其中出现的代码行*。另外,不要只记录你的catch块中有错误的事实,做一个e.printStackTrace() –
这里最有可能的情况是你的网络不是在你的设备之间路由,你的连接失败,而且你的代码不能很清楚地报告这个事实。 –