title: Android Socket Connection fail
body: <p>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</p>
  
  <p>Here is the code :</p>
  
  <pre><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(); 
      }       
  } 
  </code></pre>
  
  <p>And here is the Debugging Log: </p>
  
  <pre><code>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.&lt;init&gt;(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)
  </code></pre>