Android TCP套接字错误

时间:2016-07-20 00:24:36

标签: android tcp socketexception

我最近想在android上创建一个通过tcp连接到c#服务器的应用程序。创建服务器并侦听端口是否已打开。

问题是我的android应用程序导致的错误:

    07-20 02:11:52.057 1262-1262/connector.de.connect2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-20 02:11:52.122 1262-1262/connector.de.connect2 D/MyApp: I am here
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err: java.net.SocketException: socket failed: EACCES (Permission denied)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:619)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at java.net.PlainSocketImpl.create(PlainSocketImpl.java:198)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at java.net.Socket.startupSocket(Socket.java:584)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at java.net.Socket.tryAllAddresses(Socket.java:128)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at java.net.Socket.<init>(Socket.java:178)
07-20 02:11:52.122 1262-1262/connector.de.connect2 W/System.err:     at java.net.Socket.<init>(Socket.java:150)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at connector.de.connect2.MainActivity.onCreate(MainActivity.java:41)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.Activity.performCreate(Activity.java:6251)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.ActivityThread.-wrap11(ActivityThread.java)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5466)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err: Caused by: android.system.ErrnoException: socket failed: EACCES (Permission denied)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at libcore.io.Posix.socket(Native Method)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at libcore.io.BlockGuardOs.socket(BlockGuardOs.java:282)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:     at libcore.io.IoBridge.socket(IoBridge.java:604)
07-20 02:11:52.123 1262-1262/connector.de.connect2 W/System.err:    ... 18 more

我读到我的应用程序缺少manifest.xml中的以下代码

<uses-permission android:name="android.permission.INTERNET" />

我尝试了很多版本,包括&#34; ACCESS_NETWORK_STATE&#34;,&#34; ACCESS_NETWORK_STATE&#34;等

无论是什么都没有改变,错误仍然出现或应用程序完全崩溃。

我不知道该怎么做。

如果它也有助于应用程序本身的代码在这里:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("MyApp","I am here");
        try
        {
            Socket socket = new Socket("192.168.1.3", 1071);
            PrintWriter pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
            pw.println("Hello");
            socket.close();
        }
        catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

这里是Manifest.xml,尝试了一些改动:

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

    <uses-sdk android:minSdkVersion="14" />
    <use-permission android:name="android.permission.INTERNET" />
    <use-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <use-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

1 个答案:

答案 0 :(得分:0)

您提供的logcat输出由于某种原因未显示,但您无法在主线程上执行网络操作(例如onCreate()内部)。您需要将网络操作卸载到某种后台线程。