使用选择功能关闭服务器

时间:2018-07-09 16:24:09

标签: c sockets network-programming

因此,我是C语言套接字编程的新手,并且正在使用select函数与服务器上的多个客户端进行通信。服务器实质上只是根据需求将缓冲区回送给客户端。我已经使用Beej's guide to network programming作为服务器的模型。我不清楚的是,当发送命令退出时,我是否正在正确退出服务器。处理select函数的代码如下:

for (;;)
{
    read_fds = master; // Copy the master fds to the basic read...

    // Check to see if any flags have been set for reading
    if (select(fdmax + 1, &read_fds, NULL, NULL, NULL) == -1)
    {
        perror("select");
        exit(4);
    }
    for (i = 0; i <= fdmax; i++)
    {
        if (FD_ISSET(i, &read_fds))
        {
            if (i == listener)
            { // need to add new connnection here
                addrlen = sizeof remote_addr;
                newfd = accept(listener, (struct sockaddr *)&remote_addr, &addrlen);
                if (newfd == -1)
                {
                    perror("accept");
                }
                else
                {
                    FD_SET(newfd, &master);
                    if (newfd > fdmax)
                    {
                        fdmax = newfd;
                    }
                }

            } // end add new listener
            else
            {
                /*if (i == 0)
                {
                    printf("Input received from stdin\n");
                    continue;
                } */ 
                // handle data from existing client

                if ((nbytes = recv(i, input_buffer, sizeof input_buffer, 0)) <= 0)
                { // Remove connection if there is a hangup...
                    if (nbytes == 0)
                    {
                        printf("selectserver: socket%d hung up\n", i);
                    }
                    else
                    {
                        perror("recv");
                    }
                    close(i);
                    FD_CLR(i, &master);
                } // no bytes error or port closed - remove from fdset
                else
                {
                    if (strchr(input_buffer,'\r') == NULL){
                        printf("we have a problem\n");

                    }
                    if (strcmp(input_buffer, "exit")){

                        printf("Exit requested...\n");
                        close(listener);
                        exit(0);
                    }

                    for (j = 0; j <= fdmax; j++)
                    {
                        if (FD_ISSET(j, &master))
                        {
                            if (j != listener && j != 0)
                            {
                                if (send(j, input_buffer, nbytes, 0) == -1)
                                {
                                    error_msg = strerror(errno);
                                    printf("%s\n", error_msg);
                                    //perror("send");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

我特别关心的代码是

if (strcmp(input_buffer, "exit")){

    printf("Exit requested...\n");
    close(listener);
    exit(0);
}

其中listener是侦听套接字的文件描述符。这是退出此循环的正确方法还是有更好的方法来解决此问题?

1 个答案:

答案 0 :(得分:1)

您所做的是正确的。

关闭套接字的正确方法是使用close