Android TCP 套接字错误

Android TCP Socket Error

我最近想在 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" />

我尝试了很多版本,包括"ACCESS_NETWORK_STATE"、"ACCESS_NETWORK_STATE"等

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

我不知道该怎么办。

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

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>

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