Android Socket Connection fail

时间秒杀一切 提交于 2019-12-30 13:54:43

问题


I am trying to establish a connection between Android Client and C# Server .. My server is working fine except the android client is not working even after adding internet connection permission

Here is the code :

    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(); 
    }       
} 

And here is the Debugging Log:

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)

回答1:


By the looks of it you are trying to perform networking operation on the main thread. Take a look at the log trace specially line android.os.NetworkOnMainThreadException.

Android Developer: Network on Main Thread Exception

Always do networking on a separate thread other than main. There are different ways to accomplish that as well e.g. you can use AsyncTask or start thread on your own.

There are several posts on SO for this and Android Developer site. Here are a few links to get you going:

Android Developer: Connecting to the network

Android Developers Blog: Multithreading for performance




回答2:


Check this link here, it is because since latest version of API, it is forbidden to perform networking operation called by the main thread, so you need to create another (background) thread to perform it instead.



来源:https://stackoverflow.com/questions/13390026/android-socket-connection-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!