title: Android TCP Connection: accept() not working
body: <p>I am building an Android app using TCP connection. Below is my TCP server code. When i test, it doesn't have any errors but the app has error</p>
  
  <blockquote>
    <p>"unfortunately app has stopped".</p>
  </blockquote>
  
  <p>When i check the code it stucks at:"</p>
  
  <p><code>s=serverClient.accept();</code></p>
  
  <p>Here is my code:</p>
  
  <pre><code>package com.example.truong.total;
  
  import android.util.Log;
  import android.widget.TextView;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  /**
   * Created by duy on 26/10/2017.
   */
  
  public class TCPServer {
      int port;
      String result;
      ServerSocket serverClient;
      TextView tv5;
      MainActivity at;
      public TCPServer(int newPort,MainActivity at) {
          this.port=newPort;
          this.at=at;
          try{
              serverClient=new ServerSocket(port);
          }catch(IOException e) {
              e.printStackTrace();
          }
          Log.d("constructornethanglol","1");
          //Log.d("constructornethanglol","1");
      }
      public void setConnection() {
          tv5=(TextView)at.findViewById(R.id.textView5);
          Log.d("thanglol","2");
          try {
              Log.d("lol3","3");
              while (true) {
                  Log.d("lol4","4");
                  Socket s;
                  Log.d("thanglol1","socket");
  
                  //stuck
                  s=serverClient.accept();
  
  
                  Log.d("lol5","5");
                  BufferedReader inFromClient =
                          new BufferedReader(new InputStreamReader(s.getInputStream()));
                  this.result = inFromClient.readLine();
                  tv5.setText("lol");
                  s.close(); // sau khi dat. trong while
              }
          }catch(IOException e) {
              e.printStackTrace();
          }
      }
      public String getResult() {
          return this.result;
      }
  }
  </code></pre>
  
  <p>Can you show me where am i wrong? Thank you. Sorry for my bad English, it's not my first language.</p>