title: Socket EADDRINUSE (Address already in use)
body: <p>I am doing socket programming. I took reference from below link:</p>
  
  <p><a href="http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/" rel="noreferrer">http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/</a></p>
  
  <p>Below is detail about my issue. I have created Android lib for this ServerThread (my project requirement), and this is used in test app.</p>
  
  <p>Now test app connect to this through lib and do the process. First time it works perfectly fine, but if I closed and reopen it crashed with exception:</p>
  
  <blockquote>
    <p>"EADDRINUSE (Address already in use)"</p>
  </blockquote>
  
  <p>Also tried <code>serverSocket.setReuseAddress(true)</code> this but no luck.</p>
  
  <p>My code:</p>
  
  <pre><code>public void run() {
      Socket socket = null;
      try {
          serverSocket = new ServerSocket(SERVER_PORT);
          serverSocket.setReuseAddress(true);
  
      } catch (IOException e) {
          Log.e(TAG, "exception1= " + e.getMessage());
      }
      while (!Thread.currentThread().isInterrupted()) {
          try {
  
              socket = serverSocket.accept();
              Log.d(TAG, "server Connected.......!!!!");
  
              communicationThread = new CommunicationThread(
                      socket);
              commThread = new Thread(communicationThread);
              commThread.start();
  
          } catch (IOException e) {
              Log.e(TAG, "exception 2=" + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>If I call <code>serverSocket.close()</code> I am getting exception 2 as server socket close. Communication thread is same as given in previous link.</p>