title: Android NDK Networking problems: TCP Connection fails
body: <p>I'm having some troubles with the Android NDK.</p>
  
  <p>My project requires software components written in C that can receive data over TCP to be executed on the Android OS.</p>
  
  <p>To that end, I've made a simple server and client in C using BSD sockets and can send messages back and forth successfully across the connection on PCs. Now I've moved the clientside code into the NDK, and can't connect to my server from the emulator or from an actual android device. NDK compiles without warnings or errors, the emulator/phone are on wifi and have internet permissions enabled in the manifest. What gives? </p>
  
  <p>All it does is make a socket, fill in host/port info and attempt to connect: If it does connect, send a message, else return an error. Android always returns a negative value for connect </p>
  
  <hr>
  
  <h2>This client code, compiled/executed on Mac/Windows/Linux, works:</h2>
  
  <p>PC Client code:</p>
  
  <pre><code>int tcp_socket = socket(AF_INET, SOCK_STREAM,0); 
  if(tcp_socket&lt; 0 ) {
      cerr &lt;&lt; "Failed to create TCP socket." &lt;&lt; endl;
      return 2;
  }
  
  sockaddr_in server_tcp_add;
  server_tcp_add.sin_family = AF_INET;
  server_tcp_add.sin_port = htons(tcp_port); //set via command line: Yes, they are the same for the android
  hostent *hostp = gethostbyname(server_host);
  memcpy(&amp;server_tcp_add.sin_addr, hostp-&gt;h_addr, hostp-&gt;h_length); 
  socklen_t slen = sizeof(server_tcp_add);
  
  if(connect(tcp_socket,(sockaddr*)&amp;server_tcp_add, slen) &lt;0 ){
      cerr&lt;&lt; "Failed to connect to server with TCP. " &lt;&lt; endl;
      close(tcp_socket);
      return 3;
  }
  
  char* myString = "This is my message";
  send(tcp_socket, myString, strlen(myString), 0);
  
  close(tcp_socket);
  </code></pre>
  
  <hr>
  
  <h2>This code, executed on the Android NDK, does not</h2>
  
  <p>Code: NDK Client</p>
  
  <pre><code>jstring Java_networking_client_activate_initiateTcpConnection(JNIEnv* env, jobject javaThis){
      int tcp_socket = socket(AF_INET, SOCK_STREAM,0);
      if(tcp_socket &lt; 0){
          return (*env)-&gt;NewStringUTF(env, "ERROR CREATING SOCKET");
      }
      const char* server_host = "some.numbers.that.work"; //It's a valid IP I don't feel like sharing
      unsigned short server_port = 43000;
  
      struct sockaddr_in server_tcp_addr;
      server_tcp_addr.sin_family = AF_INET;
      server_tcp_addr.sin_port = htons(server_port);
      struct hostent *hostp = gethostbyname(server_host);
      memcpy(&amp;server_tcp_addr, hostp-&gt;h_addr, hostp-&gt;h_length);
      socklen_t slen = sizeof(server_tcp_addr);
      if(connect(tcp_socket,(struct sockaddr*)&amp;server_tcp_addr, slen) &lt; 0){ //fails here
          close(tcp_socket);
          return (*env)-&gt;NewStringUTF(env, "ERROR CONNECTING TO SERVER");
      }
  
      char* message = "hello from android!";
      send(tcp_socket, &amp;message, sizeof(message),0);
  
      return (*env)-&gt;NewStringUTF(env, "TCP message sent!");
  }
  </code></pre>
  
  <p>Largely identical; required to put struct in front of everything for NDK to compile.</p>
  
  <p>--edit--I should also mention, I have been able to send data over sockets in Java with this app. Just the C sockets that's really screwing stuff up. Even a sample of someone else that has C sockets working via NDK would be tremendously appreciated.</p>