如何使用选择和接受与客户正确沟通?

时间:2019-11-14 17:43:41

标签: c sockets

我已经阅读了一些有关const input = [['foo'],[],[],['bar']] const input2 = [{var: 'foo'}, {var: 'bar'}] const clean = (data) => data && data[0].length // check type ? data.flat(1) // use new flat method if array : (data || []).map((e) => e.var) // map objects otherwise select的示例和手册,但是我仍然无法弄清楚哪里做错了。

我试图让服务器与多个客户端通信。但是,当我先执行服务器然后执行客户端时,服务器将立即导致分段错误(当server.c中的accept时)。而且我尝试打印一些字符串以检查哪个语句导致错误,甚至在i == sockfd之后也没有打印任何内容。所以我真的不知道如何继续前进,有什么建议吗?

Server.c

if (i == sockfd)

client.c

    char inputBuffer[140] = {};
    char message[] = {"Hi,this is server.\n"};
    int sockfd = 0,forClientSockfd = 0;
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd == -1)
    {
        printf("Fail to create a socket.");
    }

    //socket creation
    struct sockaddr_in serverInfo,clientInfo;
    socklen_t addrlen = sizeof(clientInfo);
    serverInfo.sin_family = PF_INET;
    serverInfo.sin_addr.s_addr = INADDR_ANY;
    serverInfo.sin_port = htons(PORT);
    bind(sockfd,(struct sockaddr *)&serverInfo,sizeof(serverInfo));
    listen(sockfd,5);
    fd_set active_fd_set, read_fd_set;
    int i;
    struct sockaddr_in clientname;
    size_t size;

    /* Initialize the set of active sockets. */
    FD_ZERO (&active_fd_set);
    FD_SET (sockfd, &active_fd_set);
    int fd_max = sockfd;
    while (1)
    {
        /* Block until input arrives on one or more active sockets. */
        //FD_ZERO (&active_fd_set);
        //FD_SET (sockfd, &active_fd_set);
        read_fd_set = active_fd_set;
        if (select (fd_max+1, &read_fd_set, NULL, NULL, NULL) < 0)
        {
            printf("select fail\n");
        }

        /* Service all the sockets with input pending. */
        for (i = 0; i <= fd_max; ++i)
        {
            //printf("%d\n",i);
            if (FD_ISSET (i, &read_fd_set))
            {
                //printf("inner :%d %d\n",i,sockfd);
                if (i == sockfd)
                {
                    /* Connection request on original socket. */
                    //printf("A");
                    int new;
                    size = sizeof (clientname);
                    new = accept (sockfd,(struct sockaddr *) &clientname,&size);

                    if (new < 0)
                    {
                        printf("accept fail\n");
                    }
                    else
                    {
                        printf (
                            "Server: connect from host %s, port %hd.\n",
                            inet_ntoa (clientname.sin_addr),
                            ntohs (clientname.sin_port));
                        FD_SET (new, &active_fd_set);
                        if(new > fd_max)
                        {
                            fd_max = new;
                        }
                    }
                }
                else
                {
                    /* Data arriving on an already-connected socket. */
                    if (read_from_client (i) < 0)
                    {
                        close (i);
                        FD_CLR (i, &active_fd_set);
                    }
                }
            }
        }
    }
    return 0;
}

int read_from_client (int filedes)
{
    char buffer[140];
    int nbytes;

    nbytes = recv (filedes, buffer, sizeof(buffer),0);
    if (nbytes < 0)
    {
        /* Read error. */
        perror ("read");
        exit (EXIT_FAILURE);
    }
    else if (nbytes == 0)
        /* End-of-file. */
        return -1;
    else
    {
        /* Data read. */
        printf ("Server: got message: `%s'\n", buffer);
        return 0;
    }
}

谢谢!

0 个答案:

没有答案