地址已在使用中(错误号码:98)

时间:2015-04-15 08:37:05

标签: android c linux android-ndk

我收到错误: 已经在使用的地址(错误号:98),我使用的是AF_LOCAL套接字,所以我不知道哪个用户已经在本地文件或本地端口? 所以我应该检查哪个端口或哪个文件已经在使用?

代码如下: `

const char  *PATH = "/data/data/com.cxl.ipcsocket/app_local.txt";

int server_create_socket()
{
    char    buff[4096];
    int     n;
    int MAXLINE = 4096;
    int server_socket_fd, client_socket_fd;
    struct sockaddr_un server_addr;
    //Initialization socket AF_LOCAL
    if((server_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
    {
        LOGE("server create socket error: %s(errno: %d)\n",strerror(errno),errno);
        return -1;
    }

    //Initialize sockaddr etc.
    unlink (PATH);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sun_family = AF_UNIX;
    strcpy(server_addr.sun_path, PATH);

    int opt = 1;
    setsockopt(server_socket_fd, SOL_SOCKET,SO_REUSEADDR, &opt,sizeof(opt));

    //Bind the Path to socket.
    if (server_bind_socket(server_socket_fd, (struct sockaddr*) &server_addr, sizeof(server_addr)) == -1) {
        LOGE("server bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        return -1;
    }

    //Listen the socket
    if(server_listen_socket(server_socket_fd, 5) ==-1)
    {
        LOGE("server listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        return -1;
    }

    while (1) {
        if ((client_socket_fd = server_accept_socket(server_socket_fd, NULL, NULL)) < 0) {
            if (errno == EINTR)
                continue;
            else {
                LOGE("server accept socket error: %s(errno: %d)",strerror(errno),errno);
                return -1;
            }
        }
        m_channel = client_socket_fd;
        break;
    }

    while(1){
        n = server_recive_msg(client_socket_fd, buff, MAXLINE, 0);
        //send msg to client
        if(server_send_msg(client_socket_fd, "Hello,you are connected!\n", 26,0) == -1)
        {
            perror("send error");
            LOGE("server send error: %s(errno: %d)",strerror(errno),errno);
            return -1;
        }
        buff[n] = '\0';
        printf("server recv msg from client: %s\n", buff);
    }

    LOGE("server child channel fd %d>>", m_channel);

    return 1;
    }

`

0 个答案:

没有答案