简体   繁体   中英

TCP socket Transfer data from PC to android device (Android NDK)

I am working to implement TCP socket between Android device (client) and the PC (server), the code implemented by c++ (Android NDK).

every thing it's work fine, when i do run to the server side the output " server starts " 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 " socket connect to server error "

can any one tell me want is the problem?

the idea I want to transfer matrix 16*16 from server to client and form client to server...

server side:

MyServerThread::MyServerThread()
{
    startServer();
}
void MyServerThread::run()
{
    //std::cout<<"hi"<<std::endl;
    for(int i=0;i<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<<"socket creation failed"<<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*)&server_addr, sizeof(struct sockaddr)) == -1 ){
    std::cerr<<"socket bind failed"<<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<<"socket listen failed"<<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<<"server starts"<<std::endl;
    if ( (client_skt = accept(server_skt, (struct sockaddr*)&clients_addr, &temp)) == -1 ){
        std::cerr<<"socket error"<<std::endl;}
    else
    {
        gotClient=true;
        std::cout<<"Got client"<<std::endl;
    }
}

    void MyServerThread::sendMSG()
{
    if ( (numbytes = send(client_skt, buf, strlen(buf),0)) == -1){
        std::cerr<<"server, normal send error"<<std::endl;
        exit(1);
    }
}

void MyServerThread::recvMSG()
{
    if ( (numbytes = recv(client_skt, buf, bufferSize,0)) == -1 ){
        std::cerr<<"server, normal recv error"<<std::endl;
        exit(1);
    }
}

Client Side:

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 < 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 *) &local_addr, sizeof(local_addr));

    if (rc < 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 *) &serv_addr, sizeof(serv_addr));
    if (rc < 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<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]);
    }
}

Regards,

If the client and server are on the same machine, your connect() might fail with EADDRINUSE (Local address is already in use) because you're binding both client and server to the same port number:

local_addr.sin_port = htons(PORT);

Edit the client code, binding the client to port 0 (which means the kernel will pick an unused ephemeral port for you), and try again.

Even if this is not the exact problem you're having, you should always log what error was returned by library functions. After connect() fails, log the value of strerror(errno) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM