从00:00:00:00:00:00接受连接;连接后

时间:2017-11-27 13:39:20

标签: c linux sockets bluetooth

我有一个服务器套接字,它使用setsockopt监听读取超时。服务器在连接上等待一段时间,并在一段时间后接受来自“00:00:00:00:00:00之间接受的连接”的连接。如何解决这个问题。粘贴服务器代码。我收到此错误时,我没有运行客户端代码。

void * serverMainThread(void* arg)
{
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
    char buf[1024] = { 0 };
    char*tokenCommand,*tokenValue;
    int s, client, bytes_read, bytes_written, count = 0, distance;
    socklen_t opt = sizeof(rem_addr);
    struct timeval tv;
    int socketflag=true, loopflag=true;

    // allocate socket
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
    if(s<0)perror("socket call error");
    // bind socket to port 1 of the first available 
    // local bluetooth adapter
    loc_addr.rc_family = AF_BLUETOOTH;
    loc_addr.rc_bdaddr = *BDADDR_ANY;
    loc_addr.rc_channel = (uint8_t) 1;
    int b = bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
    if(b<0) perror("error in bind");
    // put socket into listening mode
    int l = listen(s, 1);
    if(l<0)perror("error in listen");
    tv.tv_sec = 65; // 30 seconds
    tv.tv_usec = 0; // microsecs, set to 0 (or ...)
    setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv,sizeof(struct timeval));

    while(true)
    {
        printf("for new process->socket count = %d", count);
        sleep(10);
        // accept one connection
        client = accept(s, (struct sockaddr *)&rem_addr, &opt);
        printf("enter a number to continue\n");
        scanf("%d",&distance);
        if(client<0) perror("server thread accept");
        int forkRet = fork();
        loopflag= socketflag = true;
        if(forkRet >= 0)
        {
            if(forkRet == 0)//child thread
            {
                childPid = getpid();
                printf("pid of new child created =%d\n",(int)childPid);
                ba2str( &rem_addr.rc_bdaddr, buf );
                fprintf(stderr, "accepted connection from %s\n", buf);
        printf("enter a number to continue\n");
        scanf("%d",&distance);
                memset(buf, 0, sizeof(buf));
            while(socketflag)
                {
                    // read data from the client
                    printf("\nbefore read\n");
                    bytes_read = read(client, buf, sizeof(buf));
                    if(bytes_read>=0) buf[bytes_read] ='\0';
                    printf("%d bytes read\n",bytes_read);
                    if(bytes_read> 0)
                    {
                        loopflag = false;
                        printf("\npid of current =%d\n",(int)childPid);
                        distance = findDistance(buf);
                        if(distance >= 10)
                        {
                            ringAlarm();
                            strcpy(buf,"Ring");
                            bytes_written = write(client, buf , strlen(buf));
                        }
                        else
                        {
                            bytes_written = write(client, "No Ring" , 8);

                        }
                    }
                    else
                    {
                        if(!loopflag)
                        {
                            printf("as loop flag is false, make socketflag false");
                            socketflag = false;
                        }  
                    }
                } 
                    close(client);
                    close(s);
                    printf("\n child process exit\n");
                    exit(0);
            }
            else//parent
            {
                wait(0);
                close(client);
                // close(s);
                printf("\n parent exit\n");
             }
        }
    }

    // close connection
    //close(client);
    //close(s);
}

0 个答案:

没有答案