Android NDK network connection issues: TCP connection not working

I have problems with Android NDK.

My project requires software components written in C that can receive TCP data that will run on Android.

To this end, I made a simple server and client in C using BSD sockets, and I can successfully send messages through the connection to the PC. Now I have moved the client code to the NDK and cannot connect to my server from the emulator or from a real Android device. NDK compiles without warnings or errors, the emulator / phone is connected to Wi-Fi and Internet access is allowed in the manifest. What gives?

All he does is make a socket, fill in the host / port information and try to connect: if he connects, send a message, still return an error. Android always returns a negative value for the connection


This client code compiled / executed on Mac / Windows / Linux works:

PC client code:

int tcp_socket = socket(AF_INET, SOCK_STREAM,0); 
if(tcp_socket< 0 ) {
    cerr << "Failed to create TCP socket." << 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(&server_tcp_add.sin_addr, hostp->h_addr, hostp->h_length); 
socklen_t slen = sizeof(server_tcp_add);

if(connect(tcp_socket,(sockaddr*)&server_tcp_add, slen) <0 ){
    cerr<< "Failed to connect to server with TCP. " << endl;
    close(tcp_socket);
    return 3;
}

char* myString = "This is my message";
send(tcp_socket, myString, strlen(myString), 0);

close(tcp_socket);

This code executed on Android NDK is not

Code: NDK Client

jstring Java_networking_client_activate_initiateTcpConnection(JNIEnv* env, jobject javaThis){
    int tcp_socket = socket(AF_INET, SOCK_STREAM,0);
    if(tcp_socket < 0){
        return (*env)->NewStringUTF(env, "ERROR CREATING SOCKET");
    }
    const char* server_host = "some.numbers.that.work"; //It 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(&server_tcp_addr, hostp->h_addr, hostp->h_length);
    socklen_t slen = sizeof(server_tcp_addr);
    if(connect(tcp_socket,(struct sockaddr*)&server_tcp_addr, slen) < 0){ //fails here
        close(tcp_socket);
        return (*env)->NewStringUTF(env, "ERROR CONNECTING TO SERVER");
    }

    char* message = "hello from android!";
    send(tcp_socket, &message, sizeof(message),0);

    return (*env)->NewStringUTF(env, "TCP message sent!");
}

In much the same way; required to place the structure in front of everything you need to compile the NDK.

- edit - I must say, I was able to send data through sockets in Java with this application. Just C sockets that really screw things up. Even a sample of someone else that has C-sockets working through NDK will be highly appreciated.

+5
1

, ...

:

memcpy(&server_tcp_addr, hostp->h_addr, hostp->h_length);

:

memcpy(&server_tcp_addr.sin_addr.s_addr, hostp->h_addr, hostp->h_length);

.

+10

All Articles