1

I'm trying to get an Android client app to talk to an Android native binary server via sockets.

My app has a button that, when pressed, sends user entered text to the server. The code is as follows:

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();
      }
    }
  }
}};

My server is written in C and cross compiled as an ARM binary. The code is as follows:

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 < 0) 
    error("ERROR opening socket");

  // Initialize socket structure
  bzero((char *) &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 *) &serv_addr,
    sizeof(serv_addr)) < 0) 
    error("ERROR on binding");
  listen(sockfd,5);
  clilen = sizeof(cli_addr);
  newsockfd = accept(sockfd, 
     (struct sockaddr *) &cli_addr, 
     &clilen);
  if (newsockfd < 0) 
    error("ERROR on accept");
  bzero(buffer,256);
  n = read(newsockfd,buffer,255);
  if (n < 0) error("ERROR reading from socket");
  printf("Here is the message: %s\n",buffer);
  n = write(newsockfd,"I got your message",18);
  if (n < 0) error("ERROR writing to socket");
  close(newsockfd);
  close(sockfd);
  return 0;
}

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?

Thanks!

user1118764
  • 9,255
  • 18
  • 61
  • 113
  • Instead of using a network socket you can also use a local socket for IPC: [Example in C](http://www.gnu.org/software/libc/manual/html_node/Local-Socket-Example.html) + Java [LocalSocket](http://developer.android.com/reference/android/net/LocalSocket.html). I seem to recall that some naming scheme needs to be used for those sockets but haven't got my old code at hand now to check. – JimmyB Jun 06 '12 at 08:39
  • Oh, and by the way: Check the local IP address _inside_ your Android "device", [see here](http://stackoverflow.com/questions/1720346/how-to-get-the-android-emulators-ip-address). – JimmyB Jun 06 '12 at 08:42

1 Answers1

0

If your client is on the same device than your server, try to change

socket = new Socket("10.0.2.2", 20002);

by :

socket = new Socket("127.0.0.1", 20002);