title: TCP socket Transfer data from PC to android device (Android NDK)
body: <p>I am working to implement TCP socket between Android device (client) and the PC (server), the code implemented by c++ (Android NDK).</p>
  
  <p>every thing it's work fine, when i do run to the server side the output "<strong>server starts</strong>" it waiting for connect to the client. and the other side when I do run for the client it work when until the connect to server and I find this error "<strong>socket connect to server error</strong>"</p>
  
  <p>can any one tell me want is the problem?</p>
  
  <p>the idea I want to transfer matrix 16*16 from server to client and form client to server...</p>
  
  <p><strong>server side:</strong></p>
  
  <pre><code>MyServerThread::MyServerThread()
  {
      startServer();
  }
  void MyServerThread::run()
  {
      //std::cout&lt;&lt;"hi"&lt;&lt;std::endl;
      for(int i=0;i&lt;16;i++)
          matrix[i]=(float)i;
      memcpy(buf,matrix,4*16);
      sendMSG();
  }
  
  //TCP socket
  /* now create the server socket
     make it an IPV4 socket (PF_INET) and stream socket (TCP)
     and 0 to select default protocol type */
  if ( (server_skt = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
      std::cerr&lt;&lt;"socket creation failed"&lt;&lt;std::endl;
      exit(1);
  }
  
  //Initail, bind to port
  /* now fill in values of the server sockaddr_in struct
     s_addr and sin_port are in Network Byte Order (Big Endian)
     Since Intel CPUs use Host Byte Order (Little Endian), conversion
     is necessary (e.g. htons(), and htonl() */
  server_addr.sin_family = AF_INET;//IPv4
  server_addr.sin_port = htons(PORT);
  server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  memset(server_addr.sin_zero, 0, 8);
  
  //binding
  
   /* now bind server port
         associate socket (server) with IP address:port (server_addr) */
  
  if ( bind(server_skt, (struct sockaddr*)&amp;server_addr, sizeof(struct sockaddr)) == -1 ){
      std::cerr&lt;&lt;"socket bind failed"&lt;&lt;std::endl;
      exit(1);
  }
  
  int queueSize=1;
  //Start listening, wait for connection from client with a pending queue of size
  if ( listen(server_skt, queueSize) == -1 ){
      std::cerr&lt;&lt;"socket listen failed"&lt;&lt;std::endl;
      exit(1);
  }
  
  //Wait for connect!
  bool gotClient=false;
  while(!gotClient){
      sin_size = sizeof(struct sockaddr_in);
      socklen_t temp=sin_size;
      std::cout&lt;&lt;"server starts"&lt;&lt;std::endl;
      if ( (client_skt = accept(server_skt, (struct sockaddr*)&amp;clients_addr, &amp;temp)) == -1 ){
          std::cerr&lt;&lt;"socket error"&lt;&lt;std::endl;}
      else
      {
          gotClient=true;
          std::cout&lt;&lt;"Got client"&lt;&lt;std::endl;
      }
  }
  
      void MyServerThread::sendMSG()
  {
      if ( (numbytes = send(client_skt, buf, strlen(buf),0)) == -1){
          std::cerr&lt;&lt;"server, normal send error"&lt;&lt;std::endl;
          exit(1);
      }
  }
  
  void MyServerThread::recvMSG()
  {
      if ( (numbytes = recv(client_skt, buf, bufferSize,0)) == -1 ){
          std::cerr&lt;&lt;"server, normal recv error"&lt;&lt;std::endl;
          exit(1);
      }
  }
  </code></pre>
  
  <p><strong>Client Side:</strong></p>
  
  <pre><code>Client::Client()
  {
  
  }
  void Client::startClient()
  {
      /* now fill in sockaddr_in for remote address */
      serv_addr.sin_family = AF_INET;
      /* save the server IP (input from Java */
      serv_addr.sin_addr.s_addr = inet_addr(serverIP);
      /* set port */
      serv_addr.sin_port = htons(PORT);
      memset(serv_addr.sin_zero, 0, 8);
  
      /* create local stream socket */
      client_skt = socket(PF_INET, SOCK_STREAM, 0);
      if (client_skt &lt; 0) {
          __android_log_print(ANDROID_LOG_ERROR,"jni client","socket creation error");
          exit(-1);
      }
  
      /* bind local socket to any port number */
      local_addr.sin_family = AF_INET;
      local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
      //local_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      local_addr.sin_port = htons(PORT);
      memset(local_addr.sin_zero, 0, 8);
  
      rc = bind(client_skt, (struct sockaddr *) &amp;local_addr, sizeof(local_addr));
  
      if (rc &lt; 0)
      {
          __android_log_print(ANDROID_LOG_ERROR,"jni client","socket bind error");
          exit(1);
      }
      __android_log_print(ANDROID_LOG_INFO,"jni client","client start finished");
  
  
  }
  void Client::connectServer()
  {
      __android_log_print(ANDROID_LOG_INFO,"jni client","connect to server starts");
      /* connect to server */
      rc = connect(client_skt, (struct sockaddr *) &amp;serv_addr, sizeof(serv_addr));
      if (rc &lt; 0)
      {
          __android_log_print(ANDROID_LOG_ERROR,"jni client","socket connect to server error");
          exit(1);
      }
      __android_log_print(ANDROID_LOG_ERROR,"jni client","server connected");
  }
  void Client::sendMSG()
  {
      if ( (numbytes = send(client_skt, "hello!", strlen("hello!"),0) )== -1 ){
          __android_log_print(ANDROID_LOG_ERROR,"jni client","send error");
          //exit(1);
      }
  
  }
  void Client::recvMSG()
  {
      if ( (numbytes = recv(client_skt, buf, bufferSize,0) ) == -1 ){
          __android_log_print(ANDROID_LOG_ERROR,"jni client","recv error");
          //exit(1);
      }
      else
      {
          memcpy (matrix, buf, numbytes);
          for(int i=0;i&lt;4;i++)
              __android_log_print(ANDROID_LOG_INFO,"matrix","%f, %f, %f, %f",matrix[i*4],matrix[i*4+1],matrix[i*4+2],matrix[i*4+3]);
      }
  }
  </code></pre>
  
  <p>Regards,</p>