Android UDP客户端无法在非root电话上接收数据

时间:2013-03-22 11:16:46

标签: android networking android-ndk udp

我在android的本机部分(即在C中)实现了UDP客户端。为了从服务器(PC)接收数据,我的设备和PC都通过wifi连接进行连接。

当客户端置于有根设备上时,它能够完美地从服务器接收数据。但是,在非root设备上,虽然它没有显示任何错误,但它无法向服务器发送/接收数据。

以下是我的服务器代码:

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/stat.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
    int cont,create_socket,new_socket,addrlen,fd;
    int bufsize = 3000;

    int nameLen=0;
    int client_address_size=0;
    char *buffer = malloc(bufsize);
    char fname[256];
    struct sockaddr_in address,client;

    if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) > 0)
    printf("The socket was created\n");

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;

    address.sin_port = htons(15000);

    if (bind(create_socket,(struct sockaddr *)&address,sizeof(address))== 0)
        printf("Binding Socket\n");

    nameLen=sizeof(address);

    if (getsockname(create_socket,(struct sockaddr *)&address,&nameLen)<0)
    {
        printf("\n\ngetsockname() error\n");
        exit(3);
    }

    printf("Port assigned is %d\n", ntohs(address.sin_port));

    client_address_size=sizeof(client);



    if(recvfrom(create_socket,fname, 255,0,(struct sockaddr *) &client,&client_address_size)<0)
    {
        printf("\n\nrecvfrom() failed\n");
        exit(4);
    }

    printf("A request for filename %s Received..\n", fname);

    if ((fd=open(fname, O_RDONLY))<0)
    {
        perror("File Open Failed");
        exit(0);
    }

    while((cont=read(fd, buffer, bufsize))>0) 
    {
        sleep(1);
        sendto(create_socket,buffer,cont,0,(struct sockaddr *) &client,client_address_size);
        printf("\n\nPacket sent\n");
    }

    sendto(create_socket,"*",1,0,(struct sockaddr *) &client,client_address_size);

    printf("Request Completed\n");
    return close(create_socket);
}
Android NDK上的

客户端代码:

JNIEXPORT void JNICALL Java_com_examplemyPlayer_startClient
  (JNIEnv *env, jclass jobj, jint portNo, jstring argv1)
{
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","start client has entered....",NULL);
    //int portno=portNo;
    const char *servIP = (*env)->GetStringUTFChars(env, argv1, 0);

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Server IP address:%s",servIP);
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Server Port No.:%d",portno);

    qBuff=(unsigned char *)malloc(sizeof(char)*Q_SIZE);//qBuff is a global pointer

    int create_socket,cont;
    int bytesWritten=0;
    int bufsize = 3000;
    int server_address_size=0;
    char *buffer = malloc(bufsize);

    char reqFname[8]="Bub.bin";

    struct sockaddr_in address,server;
    int serv_addr_size = sizeof(address);

    if ((create_socket = socket(AF_INET,SOCK_DGRAM,0)) < 0)
        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","The Socket was NOT created",NULL);

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","The Socket was successfully created",NULL);

    address.sin_family = AF_INET;
    address.sin_port = htons(15000);
    address.sin_addr.s_addr=inet_addr(servIP);

    if((sendto(create_socket, reqFname, sizeof(reqFname), 0,(struct sockaddr *) &address,sizeof(address)))==-1)
        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Error sending filename to server",NULL);

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Request Accepted... \n\nReceiving File...",NULL);

    server_address_size=sizeof(server);

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Contents of the file are:",NULL);

    while((cont=recvfrom(create_socket, buffer, bufsize, 0,(struct sockaddr *) &address,&serv_addr_size))>0)
    {
            if(buffer[cont-1]=='*')
               break;

            memcpy(qBuff+r,buffer,cont);//copying received data to a circular queue
            r=(r+3000)%Q_SIZE;
            __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","Bytes received:%d",cont);
            //write(1, buffer, cont);
    }

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","EOF",NULL);
    free(qBuff);
    close(create_socket);

}

以上代码适用于root设备,但不适用于无根设备。由于我正在创建的应用程序应该能够在root和unrooted设备上运行,我还需要在无根设备上运行代码。

只有在使用 / sbin / services iptable stop 拨打Linux防火墙后,服务器才能连接到root用户设备。那么是否存在阻止通信端口的无根设备的防火墙。在上面的代码我尝试端口没有15000.但我也尝试了其他端口,但没有锻炼。

所以我请求读者提供有用的答案或参考链接。提前谢谢。

0 个答案:

没有答案