title: tcp socket problem on android
body: <p>I need to use the tcp socket connection to get the data from a bluebox, if I input a comment, such as "getcolor", the bluebox will send me the information like"red, blue".
  In this case the bluebox as a server and I do not need to program on it, but I have problem to show the information on the EditText.</p>
  
  <pre><code>public class sender {
  public static void main(String[] args)throws IOException{
  
          Socket socket = new Socket("192.168.1.176",14111);
  
          OutputStream out = socket.getOutputStream();
  
          BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
          BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));         
           PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);
  
           String buffer = new String("");
           String ms = "";
        while(true)
        {
            while(in.ready())
  
                buffer+= in.readLine()+ "\n";
            String[] line = buffer.split("\n");
  
            while(msg.ready())
                ms = msg.readLine();
            if(ms.equals("exit"))
            {
                break;
            }
            if(!ms.equals(""))
            {
                ou.println(ms);
                ou.flush();
                ms = "";
            }
            if(!buffer.equals(""))
            {
  
                System.out.print(buffer);
                buffer = "";
  
            }
  
        } 
  
        in.close();
        out.close();
        socket.close();
  
  }
  }
  </code></pre>
  
  <p>this java code works, but it fails in the android code below:</p>
  
  <pre><code>public class BlueBoxApp extends Activity {
  /** Called when the activity is first created. */
  
  Context appInstance = this;
  private EditText info;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
             info = (EditText)findViewById(R.id.EditText01);
      try{
  
          InetAddress serverAddr = InetAddress.getByName("192.168.1.176");//TCP服务器IP地址
  
              Log.d("TCP", "server,receiving...");
  
            Socket socket = new Socket(serverAddr,14111);
  
  
             try {   
  
  
             OutputStream out = socket.getOutputStream();
             BufferedReader msg = new BufferedReader(new InputStreamReader(System.in)); 
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));          
           PrintWriter ou = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)),true);
  
              String buffer = new String("");
              String ms = "getsensorno";
              Log.d("TCP", "sending:'"+ms+"'");
          while(true)
          {
            while(in.ready())
  
                buffer+= in.readLine()+ "\n";
  
            while(msg.ready())
                ms = msg.readLine();
            if(ms.equals("exit"))
            {
                break;
            }
            if(!ms.equals(""))
            {
                ou.println(ms);
                ou.flush();
                ms = "";
            }
            if(!buffer.equals(""))
            {
  
               info.setText(buffer);
              buffer = "";
  
            } 
          } 
  
              } catch (Exception e) {
  
              Log.e("TCP", "error",e);
  
          }finally{
  
              socket.close();
  
          }
  
          }catch(Exception e){
  
             Log.e("TCP", "error",e);
  
          }
  }
  }
  </code></pre>
  
  <p>what is the problem and how to set a thread for it? Thanks! </p>