title: Android TCP client socket
body: <p>In my server I send to socket an infinite cycle of ("number: %d\n",i). And, at each cycle, I increase the number by one. I then use the following code to receive it in the android device (which is the TCP client):
            while(connected){             </p>
  
  <pre><code>            //receive data through socket  
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              Log.d(TAG, "1");
  
               while(connected){              
  
              //receive data through socket  
              BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              Log.d(TAG, "1");
  
               while ( (line = in.readLine()) != null){
  
                     Log.d(TAG, line);
                     str.append(line);
                     Log.d(TAG, "2");
  
                     runOnUiThread(new Runnable() {
  
                      @Override
                      public void run() {
  
                          input.setText( str );
  
                  }});
  
               }
  
  
          }
  </code></pre>
  
  <p>this is the result of the logcat:</p>
  
  <blockquote>
    <p>r: 25363332´┐¢number: 3405er: 3839er: 4201er: 4925´┐¢number: 55776228´┐¢number:
    6301´┐¢number: 7265´┐¢number: 8351number: 9170number: 9414number: 97765102101057
    210934er: 11079er: 11441´┐¢number: 1173112020´┐¢number: 12093´┐¢number: 12455num
    ber: 12672er: 13491´┐¢number: 13903er: 14337er: 14699´┐¢number: 15351´┐¢number:
    15713er: 1614716364´┐¢number: 1643716726´┐¢number: 16799number: 17016number: 173
    787´┐¢number: 17885number: 187047´┐¢number: 19695er: 201291´┐¢number: 20419´┐¢nu
    mber: 20781´┐number: 21143er: 21215er: 2157721794´┐¢number: 21867er: 2230122518n
    umber: 22808er: 2362724446´┐¢number: 25125er: 2555926378727948´┐¢number: 28383er
    : 284557number: 28600286729number: 2896229396er: 295413´┐¢number: 30433number: 3</p>
  </blockquote>
  
  <p>So this is not recognizing the \n, and between socket readings it is losing data. Is it because of the constant actualization of the UI thread?</p>
  
  <p>How should I do this instead?</p>
  
  <p>UPDATE</p>
  
  <p>this is the server code in C:</p>
  
  <pre><code> int main(int argc, char **argv)
   {
  
  socklen_t client_len;
  struct sockaddr_in cli_addr;
  struct sockaddr_in serv_addr;
  int client_count=0;
  int nbytes, i, sockfd, new_sockfd;
  char buf[20];
  
  /* Init socket */   
  sockfd=socket(AF_INET, SOCK_STREAM, 0);     
  if (sockfd&lt;0) error("ERROR opening socket");
  bzero((char *) &amp;serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons( atoi(argv[1]) );
  
  /*associate socket to the port*/
  if (bind(sockfd, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr)) &lt; 0) error("ERROR on binding");
  
  /* connection to the client */
  listen(sockfd, 1); /*server is ready to receive connections*/
  client_len = sizeof(cli_addr);
  new_sockfd = accept(sockfd, (struct sockaddr *) &amp;cli_addr, &amp;client_len); /*server accepts request */
  client_count =1; /*write to socket when client is available*/   
  
  i = 1;
  
  while(1){
  
  
      sprintf(buf, "number: %d\n", i); 
  
      if (client_count &gt;0){
          nbytes = write( new_sockfd, buf, sizeof(buf)); /* write to socket */
          if(nbytes&lt;0){
              printf("ERROR on sendto");
          }
      }
  
      i = i+1;
  }   
  
  return (0); 
  </code></pre>
  
  <p>}</p>