0

我正在尝试在 Android 客户端和 C# 服务器之间建立连接。我的服务器工作正常,除了 android 客户端即使在添加互联网连接权限后也无法工作

这是代码:

    private void connectSocket(String a){ 

    try { 
        InetAddress serverAddr = InetAddress.getByName("192.168.0.2"); 
        Log.d("TCP", "C: Connecting....");

        Socket socket = new Socket(serverAddr,4444); 
        Log.d("TCP", "C: I dunno ...");
        //String message = "1";

        PrintWriter out = null;
        BufferedReader in = null;

        try { 
          //  Log.d("TCP", "C: Sending: '" + message + "'"); 
            out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true); 
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));                

            //out.println(message);
            while ((in.readLine()) != null) {
            txt.append(in.readLine());
            }

            Log.d("TCP", "C: Sent."); 
            Log.d("TCP", "C: Done.");               

        } catch(Exception e) { 
            Log.e("TCP", "S: Error", e); 
        } finally { 
            socket.close(); 
        } 

    } catch (UnknownHostException e) { 
        // TODO Auto-generated catch block 
        Log.e("TCP", "C: UnknownHostException", e); 
        e.printStackTrace(); 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        Log.e("TCP", "C: IOException", e); 
        e.printStackTrace(); 
    }       
} 

这是调试日志:

11-15 02:41:58.040: W/dalvikvm(26839): threadid=1: thread exiting with uncaught exception (group=0x41c352a0)
11-15 02:41:58.075: E/AndroidRuntime(26839): FATAL EXCEPTION: main
11-15 02:41:58.075: E/AndroidRuntime(26839): android.os.NetworkOnMainThreadException
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at libcore.io.IoBridge.connect(IoBridge.java:112)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.net.Socket.startupSocket(Socket.java:566)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.net.Socket.<init>(Socket.java:225)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at com.example.socketclient.SocketCode.connectSocket(SocketCode.java:50)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at com.example.socketclient.SocketCode.access$0(SocketCode.java:44)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at com.example.socketclient.SocketCode$1.onClick(SocketCode.java:35)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.view.View.performClick(View.java:4211)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.view.View$PerformClick.run(View.java:17267)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.os.Handler.handleCallback(Handler.java:615)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.os.Looper.loop(Looper.java:137)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at android.app.ActivityThread.main(ActivityThread.java:4898)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.lang.reflect.Method.invokeNative(Native Method)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at java.lang.reflect.Method.invoke(Method.java:511)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-15 02:41:58.075: E/AndroidRuntime(26839):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

7

从外观上看,您正试图在主线程上执行网络操作。看一下 log trace 特别行android.os.NetworkOnMainThreadException

Android 开发人员:主线程异常上的网络

始终在主线程以外的单独线程上进行网络连接。也有不同的方法可以实现这一点,例如,您可以使用AsyncTask或自行启动线程。

对于这个和 Android 开发者网站,有几篇关于 SO 的帖子。这里有一些链接可以帮助您:

Android 开发者:连接网络

Android 开发者博客:多线程提高性能

于 2012-11-15T01:05:42.153 回答
0

在这里查看这个链接,因为从最新版本的API开始,禁止执行主线程调用的网络操作,所以您需要创建另一个(后台)线程来执行它。

于 2012-11-15T01:02:15.233 回答