title: Android app talking to Android native binary via sockets
body: <p>I'm trying to get an Android client app to talk to an Android native binary server via sockets.</p>
  
  <p>My app has a button that, when pressed, sends user entered text to the server. The code is as follows:</p>
  
  <pre><code>Button.OnClickListener buttonSendOnClickListener = new Button.OnClickListener(){
    public void onClick(View arg0) {
      Socket socket = null;
      DataOutputStream dataOutputStream = null;
      DataInputStream dataInputStream = null;
  
      try {
        socket = new Socket("10.0.2.2", 20002);
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataInputStream = new DataInputStream(socket.getInputStream());
        dataOutputStream.writeUTF(textOut.getText().toString());
      } catch (UnknownHostException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      finally{
        if (socket != null){
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
  
      if (dataOutputStream != null){
        try {
          dataOutputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
  
      if (dataInputStream != null){
       try {
          dataInputStream.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }};
  </code></pre>
  
  <p>My server is written in C and cross compiled as an ARM binary. The code is as follows:</p>
  
  <pre><code>int main(int argc, char ** argv)
  {
    int sockfd, newsockfd, portno;
    socklen_t clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n;
  
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd &lt; 0) 
      error("ERROR opening socket");
  
    // Initialize socket structure
    bzero((char *) &amp;serv_addr, sizeof(serv_addr));
    portno = 20002;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
  
    if (bind(sockfd, (struct sockaddr *) &amp;serv_addr,
      sizeof(serv_addr)) &lt; 0) 
      error("ERROR on binding");
    listen(sockfd,5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, 
       (struct sockaddr *) &amp;cli_addr, 
       &amp;clilen);
    if (newsockfd &lt; 0) 
      error("ERROR on accept");
    bzero(buffer,256);
    n = read(newsockfd,buffer,255);
    if (n &lt; 0) error("ERROR reading from socket");
    printf("Here is the message: %s\n",buffer);
    n = write(newsockfd,"I got your message",18);
    if (n &lt; 0) error("ERROR writing to socket");
    close(newsockfd);
    close(sockfd);
    return 0;
  }
  </code></pre>
  
  <p>I run the server using an adb shell, and it runs till the accept() call, waiting them for a connection. I then run the client app and then tap the send button. However, it doesn't seem like anything is sent. Is there something I'm doing wrong here? e.g. wrong IP address for localhost, or using AF_INET instead of PF_UNIX or something?</p>
  
  <p>Thanks!</p>