title: Android TCP Client in phone has problems in communicating with external server
body: <p>I have a TCP Client in Android (Java program in Eclipse). The server was another Java app running in Eclipse. Everything works fine in this situation.</p>
  
  <p>When I tried to receive message from my colleague's app (developed in Rhapsody and I think C++), I receive the message only after his app is closed and not while his app is running and sending messages. Do you have any idea why this happens?</p>
  
  <p>Thank you for the time and effort on this.</p>
  
  <p>Cheers,
  Madhu</p>
  
  <p>The java server is like this: </p>
  
  <pre><code>public class TCPSendServer implements Runnable{
      public static final String SERVERIP = "192.168.178.24";
      public static final int SERVERPORT = 1200;
      //static Category cat = Category.getInstance(TCPSendServer.class.getName());
      //cat.debug("Start of main()");
  
      public void run() {
           try {
               System.out.println("S: Connecting...");             
               ServerSocket serverSocket = new ServerSocket(SERVERPORT);
               String msg = "&lt;MSG&gt;&lt;N&gt;shiftDirection&lt;/N&gt;&lt;V&gt;1&lt;/V&gt;&lt;/MSG&gt;";
               String msg1 = "&lt;MSG&gt;&lt;N&gt;vehicleSpeed&lt;/N&gt;&lt;V&gt;120&lt;/V&gt;&lt;/MSG&gt;";
               String msg2 = "SD&lt;!N&gt;&lt;V&gt;0&lt;!V&gt;&lt;!MSG&gt;";
  
               //while (true) {
                   Socket client = serverSocket.accept();
  
                    try {
                        System.out.println("S: Sending: '" + msg + "'");
                        PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true);                    
                        Thread.sleep (5000);
                        out.println(msg);
                        Thread.sleep (5000);
                        //out.println(msg2);
                        Thread.sleep (5000);
                        out.println(msg1);
                        //out.flush();
                        System.out.println("S: Sent.");
                        System.out.println("S: Done.");
                      } catch(Exception e) {
                          System.out.println("S: Error");
                          e.printStackTrace();
                      } //finally {
                        //    client.close();                        
                      //}  
               //}
           } catch (Exception e) {
               System.out.println("S: First try error");
               e.printStackTrace();
           }
      }
  
      public static void main (String a[]) {
          Thread desktopServerThread = new Thread(new TCPSendServer());
          desktopServerThread.start();
      }
  }
  </code></pre>
  
  <p>The Android client code:
  Main activity:</p>
  
  <pre><code>public class TCPListen extends Activity implements TCPListener {
      private TextView mTitle;
      public String data[] = new String[2];
  
      /** Called when the activity is first created. */
       @Override  
           public void onCreate(Bundle savedInstanceState) {  
               super.onCreate(savedInstanceState);  
               //setContentView(R.layout.main);            
  
            // Set up the window layout
               requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
               setContentView(R.layout.main);
               getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
  
               // Set up the custom title
               mTitle = (TextView) findViewById(R.id.title_left_text);
               mTitle.setText(R.string.app_name);
               mTitle = (TextView) findViewById(R.id.title_right_text);
  
               //TcpServiceHandler handler=new TcpServiceHandler(this);  
               //handler.execute("192.168.178.24");  
  
               TcpServiceHandler handler = new TcpServiceHandler(this,this);  
               Thread th = new Thread(handler);  
               th.start();             
           }          
  
           public String[] callCompleted(String source){ 
                  Log.d("TCP", "Std parser " + source);
                  mTitle.setText(source);
                  //String data[] = new String[2]; 
  
                  //if (source.matches("&lt;MSG&gt;&lt;N&gt;.*&lt;/N&gt;&lt;V&gt;.*&lt;/V&gt;&lt;/MSG&gt;"))  {           
                      Document doc = null;  
                      try{
                         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  
                         DocumentBuilder db = dbf.newDocumentBuilder();  
                         doc = (Document) db.parse(new ByteArrayInputStream(source.getBytes()));  
                         NodeList n = doc.getElementsByTagName("N");  
                         Node nd = n.item(0);  
                         String msgName = nd.getFirstChild().getNodeValue();  
                         NodeList n1 = doc.getElementsByTagName("V");  
                         Node nd1 = n1.item(0);  
                         String tmpVal = nd1.getFirstChild().getNodeValue();  
                         data[0] = msgName;  
                         data[1] = tmpVal; 
                         Log.d("TCP", "Inside Std parser " + data[0] + " " + data[1]);
                         //actionOnData(data[0], data[1]);
                        }  
                      catch(Exception e){  
                      e.printStackTrace();  
                  }
                  Log.d("TCP", "Just outside Std parser " + data[0] + " " + data[1]);
                  return data;
                  //} else Log.d("TCP", "Message in wrong format " + source);
                  //mTitle.setText("Message in wrong format " + source);
                  //return data;
              }
  </code></pre>
  
  <p>Interface:</p>
  
  <pre><code>public interface TCPListener {
      public String[] callCompleted(String msg);
  }
  </code></pre>
  
  <p>TCPServiceHandler:</p>
  
  <pre><code>public class TcpServiceHandler implements Runnable {  
       TCPListener _listener;            
       private Activity _act;  
       public TcpServiceHandler(TCPListener listener, Activity act){    
           _listener = listener;  
           _act = act;  
       }        
  
       public synchronized void run() {  
           // TODO Auto-generated method stub           
           //if(socket==null){    
               try {  
                   InetAddress serverAddr = InetAddress.getByName("192.168.178.25");  
                   Socket socket = new Socket(serverAddr, 1200);  
           //  
                   while(true){  
                    try {                           
                           BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));  
                           final String str = in.readLine();  
                           this._act.runOnUiThread(new Runnable(){  
  
                           public void run() {  
                               _listener.callCompleted(str);  
                               }                                
                           });                                                  
                    }  
                    catch(Exception e){  
                        e.printStackTrace();  
                    }  
                   }  
               } catch (UnknownHostException e) {  
                   // TODO Auto-generated catch block  
                   e.printStackTrace();  
               } catch (IOException e) {  
                   // TODO Auto-generated catch block  
                   e.printStackTrace();  
               }  
       }    
   }
  </code></pre>