title: Why I get nothing back from node.js server on android
body: <p>I'm developing a android app with Socket connections. I followed a tutorial on the Internet with a sample server and android client (<a href="http://myandroidsolutions.blogspot.nl/2012/07/android-tcp-connection-tutorial.html" rel="nofollow">http://myandroidsolutions.blogspot.nl/2012/07/android-tcp-connection-tutorial.html</a>). </p>
  
  <p>the tutorial worked perfectly.</p>
  
  <p>But I'm trying to connect to a Node.js socket io server with same android client contact. I can send messages with it but I can not received while the server sends a ping every second.</p>
  
  <p>Why its not working? 
  what am I doing wrong?</p>
  
  <p>The Node.js socket io server is working fine, the iPhone version of the app can send and receive.</p>
  
  <p>All ports are open.</p>
  
  <p>Can someone help me please? Thank u!</p>
  
  <p><strong>Source</strong></p>
  
  <p>TCPClient.java</p>
  
  <pre><code> public class TCPClient {
  
      private String serverMessage;
      public static final String SERVERIP = "*ip-addres*"; //your computer IP address
      public static final int SERVERPORT = 4444;
      private OnMessageReceived mMessageListener = null;
      private boolean mRun = false;
  
      PrintWriter out;
      BufferedReader in;
  
      /**
       *  Constructor of the class. OnMessagedReceived listens for the messages received from server
       */
      public TCPClient(OnMessageReceived listener) {
          mMessageListener = listener;
      }
  
      /**
       * Sends the message entered by client to the server
       * @param message text entered by client
       */
      public void sendMessage(String message){
          if (out != null &amp;&amp; !out.checkError()) {
              out.println(message);
              out.flush();
          }
      }
  
      public void stopClient(){
          mRun = false;
      }
  
      public void run() {
  
          mRun = true;
  
          try {
              //here you must put your computer's IP address.
              InetAddress serverAddr = InetAddress.getByName(SERVERIP);
  
              Log.e("TCP Client", "C: Connecting...");
  
              //create a socket to make the connection with the server
              Socket socket = new Socket(serverAddr, SERVERPORT);
  
              try {
  
                  //send the message to the server
                  out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
  
                  Log.e("TCP Client", "C: Sent.");
  
                  Log.e("TCP Client", "C: Done.");
  
                  //receive the message which the server sends back
                  in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
                  //in this while the client listens for the messages sent by the server
                  while (mRun) {
                      serverMessage = in.readLine();
  
                      if (serverMessage != null &amp;&amp; mMessageListener != null) {
                          //call the method messageReceived from MyActivity class
                          mMessageListener.messageReceived(serverMessage);
                      }
                      serverMessage = null;
  
                  }
  
  
                  Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + serverMessage + "'");
  
  
              } catch (Exception e) {
  
                  Log.e("TCP", "S: Error", e);
  
              } finally {
                  //the socket must be closed. It is not possible to reconnect to this socket
                  // after it is closed, which means a new socket instance has to be created.
                  socket.close();
              }
  
          } catch (Exception e) {
  
              Log.e("TCP", "C: Error", e);
  
          }
  
      }
  
      //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
      //class at on asynckTask doInBackground
      public interface OnMessageReceived {
          public void messageReceived(String message);
      }}
  </code></pre>
  
  <p>ChatActivity.java</p>
  
  <pre><code>    public class ChatActivity extends Activity{
   private ListView mList;
      private ArrayList&lt;String&gt; arrayList;
      private MyCustomAdapter mAdapter;
      private TCPClient mTcpClient;
      @Override
      public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_chat);
  
          arrayList = new ArrayList&lt;String&gt;();
  
          final EditText editText = (EditText) findViewById(R.id.editText);
          Button send = (Button)findViewById(R.id.send_button);
  
          //relate the listView from java to the one created in xml
          mList = (ListView)findViewById(R.id.list);
          mAdapter = new MyCustomAdapter(this, arrayList);
          mList.setAdapter(mAdapter);
  
          // connect to the server
          new connectTask().execute("");
  
          send.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  String message = editText.getText().toString();
  
                  //add the text in the arrayList
                  arrayList.add("c: " + message);
  
                  //sends the message to the server
                  if (mTcpClient != null) {
                      mTcpClient.sendMessage(message);
                  }
  
                  //refresh the list
                  mAdapter.notifyDataSetChanged();
                  editText.setText("");
              }
          });
  
      }
  
      public class connectTask extends AsyncTask&lt;String,String,TCPClient&gt; {
  
          @Override
          protected TCPClient doInBackground(String... message) {
  
              //we create a TCPClient object and
              mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
                  @Override
                  //here the messageReceived method is implemented
                  public void messageReceived(String message) {
                      //this method calls the onProgressUpdate
                      publishProgress(message);
                  }
              });
              mTcpClient.run();
  
              return null;
          }
  
          @Override
          protected void onProgressUpdate(String... values) {
              super.onProgressUpdate(values);
  
              //in the arrayList we add the messaged received from server
              arrayList.add(values[0]);
              // notify the adapter that the data set has changed. This means that new message received
              // from server was added to the list
              mAdapter.notifyDataSetChanged();
          }
      }
  }
  </code></pre>
  
  <p>Node JS Server</p>
  
  <pre><code>    var net = require('net');
  var mysql = require('mysql');
  var colorize = require('colorize');
  var cconsole = colorize.console;
  
  var clients = [];
  var count = 0;
  
  var messages = [];
  
  function create_id()
  {
          return count += 1;
  }
  
  setInterval(function()
  {
          for(i = 0; i &lt; clients.length; i++)
          {
                  var client = clients[i];
  
                  var params = {};
                  params.type = "ping";
  
                  if(client.socket.write(JSON.stringify(params)))
                  {
                          console.log("ping send");
                  }
                  else
                  {
                          clients.splice(i, 1);
                  }
          }
  }, 1000);
  
  var server = net.createServer(function( socket )
  {      
          cconsole.log("#red[Client connected to the server with ip: " + socket.remoteAddress+"]");
  
          socket.on("error",function(error)
          {
                  console.log("error" + error);
          });
  
          socket.on("close",function()
          {
                  cconsole.log("#red[Client has disconnected]");
          });
  
          socket.on("data",function(data)
          {        
                  try
                  {
                          var packet = JSON.parse(data);
  
                          if(packet.type == "register")
                          {
                                  var client = [];
  
                                  client.socket = socket;
                                  client.clientID = create_id();
                                  client.username = packet.username;
  
                                  messages[packet.username] = [];
  
                                  clients.push(client);
  
                                  var params = {};
  
                                  params.type = "register";
                                  params.clientID = client.clientID;
  
                                  socket.write(JSON.stringify(params));
  
                                  console.log("Registered client : " + params.clientID);         
                          }
  
                          if(packet.type == "online")
                          {
                                  var params = {};
                                  var identifiers = [];
  
                                  for(i = 0; i &lt; clients.length; i++)
                                  {
  
                                          identifiers.push(clients[i].clientID);
                                  }
  
                                  params.type = "online";
                                  params.clients = identifiers;
  
                                  socket.write(JSON.stringify(params));
                          }
  
                          if(packet.type == "message")
                          {
                                  var client = packet.sender;
                                  var recipient = packet.recipient
  
                                  for(i = 0; i &lt; clients.length; i++)
                                  {
                                          if(clients[i].clientID.toString() == recipient.toString())
                                          {
                                                  var params = {}
  
                                                  params.type = "message";
                                                  params.sender = packet.sender;
                                                  params.recipient = packet.recipient;
                                                  params.message = packet.message;
  
                                                  clients[i].socket.write(JSON.stringify(params));
  
                                                  console.log("Wrote message " + params.message + " from sender " + params.sender +  " to recipient " + recipient)
                                                  return;
                                          }
                                  }
                                          console.log("Recipient was not valid");                        
                          }
                  }
                  catch(e)
                  {
                          cconsole.log(e.message);
                  }
          });    
  });
  
  server.listen(8124,"&lt;Server IP&gt;", function()
  {      
          //'listening' listener
          cconsole.log('Server is listening for incoming connections');
  });
  </code></pre>