title: Android socket ConnectException ENETUNREACH
body: <p>I am trying to connect between 2 Android devices (Moto G and Nexus 5) using Socket connections over a common wifi connection.</p>
  
  <p>I have a application which runs in either server/client mode. The app is running on both devices.</p>
  
  <p>When i set my MotoG to be the server i am able to send data from my client app (Nexus) perfectly. However, this is not working in the reverse scenario. I end up getting the following error - </p>
  
  <blockquote>
    <p>java.net.ConnectException: failed to connect to SERVERIP (port 8080): connect failed: ENETUNREACH (Network is unreachable)</p>
  </blockquote>
  
  <p><strong>Server code -</strong> </p>
  
  <pre><code>public class ServerThread implements Runnable {
  
      public void run() {
          try {
              if (SERVERIP != null) {
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          serverStatus
                                  .setText("Listening on IP: " + SERVERIP);
                      }
                  });
                  serverSocket = new ServerSocket(8080);
                  while (true) {
                      // LISTEN FOR INCOMING CLIENTS
                      Socket client = serverSocket.accept();
                      handler.post(new Runnable() {
                          @Override
                          public void run() {
                              serverStatus2.setText("Connected.");
                          }
                      });
  
                      try {
                          BufferedReader in = new BufferedReader(
                                  new InputStreamReader(
                                          client.getInputStream()));
                          String line = null;
                          while ((line = in.readLine()) != null) {
                              final String currLine = line;
                              Log.i("Server Activity",currLine);
                              handler.post(new Runnable() {
                                  @Override
                                  public void run() {
                                      String origString = serverStatus2.getText().toString();
                                      String newString = origString + "\n" + currLine;
                                      serverStatus2.setText(newString);
                                      ToneGenerator tone = new ToneGenerator(AudioManager.STREAM_NOTIFICATION,90);
                                      tone.startTone(ToneGenerator.TONE_PROP_ACK,10);
                                  }
                              });
                          }
                          break;
                      } catch (Exception e) {
                          handler.post(new Runnable() {
                              @Override
                              public void run() {
                                  serverStatus
                                          .setText("Oops. Connection interrupted. Please reconnect your phones.");
                              }
                          });
                          e.printStackTrace();
                      }
                  }
              } else {
                  handler.post(new Runnable() {
                      @Override
                      public void run() {
                          serverStatus
                                  .setText("Couldn't detect internet connection.");
                      }
                  });
              }
          } catch (final Exception e) {
              handler.post(new Runnable() {
                  @Override
                  public void run() {
                      serverStatus.setText("Error" + e.getMessage());
                  }
              });
              e.printStackTrace();
          }
      }
  }
  
  // GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK
  private String getLocalIpAddress() {
      try {
          for (Enumeration&lt;NetworkInterface&gt; en = NetworkInterface
                  .getNetworkInterfaces(); en.hasMoreElements();) {
              NetworkInterface intf = en.nextElement();
              for (Enumeration&lt;InetAddress&gt; enumIpAddr = intf
                      .getInetAddresses(); enumIpAddr.hasMoreElements();) {
                  InetAddress inetAddress = enumIpAddr.nextElement();
                  if (!inetAddress.isLoopbackAddress()) {
                      return inetAddress.getHostAddress().toString();
                  }
              }
          }
      } catch (SocketException ex) {
          Log.e("ServerActivity", ex.toString());
      }
      return null;
  }
  
  @Override
  protected void onStop() {
      super.onStop();
      try {
          // MAKE SURE YOU CLOSE THE SOCKET UPON EXITING
          serverSocket.close();
      } catch (IOException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>Client -</p>
  
  <pre><code>    public class ClientThread implements Runnable {
  
      public void run() {
          try {
              if (socket==null){
                  initSocketConn();
              }
              //connected = true;
              //while (connected) {
                  try {
                      Log.d("ClientActivity", "C: Sending command.");
                      if (out==null){
                          out = new PrintWriter(
                                  new BufferedWriter(new OutputStreamWriter(
                                          socket.getOutputStream())), true);
                      }
                      // WHERE YOU ISSUE THE COMMANDS
                      out.println(edtMessage.getText().toString());
                      Log.d("ClientActivity", "C: Sent.");
                  } catch (Exception e) {
                      Log.e("ClientActivity", "S: Error", e);
                  }
              //}
              //socket.close();
              Log.d("ClientActivity", "C: Closed.");
          } catch (Exception e) {
              Log.e("ClientActivity", "C: Error", e);
              connected = false;
          }
      }
  }
  
  private void initSocketConn(){
      try{
          InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
          Log.d("ClientActivity", "C: Connecting...");
          socket = new Socket(serverAddr,
                  8080);  //THIS IS WHERE THE ERROR IS THROWN
      }
      catch (Exception ex){
          ex.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I have checked online &amp; tried different options like using different port number but it has not worked. I am confused why the code works perfectly when the Moto is server but gives the error when NExus is Server??? Please help!</p>
  
  <p><strong>Logcat -</strong> </p>
  
  <pre><code>03-29 07:25:06.065 20592-20611/com.shriwas.samplesocketapp D/OpenGLRenderer: endAllActiveAnimators on 0xb7ca02d8 (RippleDrawable) with handle 0xb7d24240
  03-29 07:25:08.957 20592-21470/com.shriwas.samplesocketapp D/ClientActivity: C: Connecting...
  03-29 07:25:08.958 20592-21470/com.shriwas.samplesocketapp W/System.err: java.net.ConnectException: failed to connect to /fe80::8c3a:e3ff:fe5d:2e0d%p2p0%25 (port 8080): connect failed: ENETUNREACH (Network is unreachable)
  03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:124)
  03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
  03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:163)
  03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err:     at java.net.Socket.startupSocket(Socket.java:590)
  03-29 07:25:08.963 20592-21470/com.shriwas.samplesocketapp W/System.err:     at java.net.Socket.&lt;init&gt;(Socket.java:226)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at com.shriwas.samplesocketapp.MyClientActivity.initSocketConn(MyClientActivity.java:108)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at com.shriwas.samplesocketapp.MyClientActivity.access$300(MyClientActivity.java:20)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at com.shriwas.samplesocketapp.MyClientActivity$ClientThread.run(MyClientActivity.java:77)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at java.lang.Thread.run(Thread.java:818)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err: Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at libcore.io.Posix.connect(Native Method)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
  03-29 07:25:08.964 20592-21470/com.shriwas.samplesocketapp W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
  03-29 07:25:08.965 20592-21470/com.shriwas.samplesocketapp W/System.err:    ... 8 more
  03-29 07:25:08.966 20592-21470/com.shriwas.samplesocketapp D/ClientActivity: C: Sending command.
  03-29 07:25:08.971 20592-21470/com.shriwas.samplesocketapp E/ClientActivity: S: 
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method
    'java.io.OutputStream java.net.Socket.getOutputStream()' on a null
    object reference<br>
    at
    com.shriwas.samplesocketapp.MyClientActivity$ClientThread.run(MyClientActivity.java:84)
    at java.lang.Thread.run(Thread.java:818)
        03-29 07:25:08.971 20592-21470/com.shriwas.samplesocketapp D/ClientActivity: C: Closed.</p>
  </blockquote>