使用 select 函数关闭服务器

标签 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是监听套接字的文件描述符。这是退出此循环的正确方法还是有更好的方法来处理这个问题?

最佳答案

你所做的是正确的。

关闭套接字的正确方法,无论是连接的套接字还是监听套接字,都是使用close

关于使用 select 函数关闭服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250075/

相关文章:

iphone - 应用程序在后台时如何处理套接字连接事件?

c++ - 我应该考虑网络字节顺序吗?

c++ - 使用 Boost.Asio 强制异步套接字读取提前完成

c - 解析内存映射文件C

c - openLDAP c 客户端协议(protocol)错误 (ldap_simple_bind_s : Protocol error)

c - 隐藏命令行程序中的用户输入

c - UNIX 非阻塞 I/O : O_NONBLOCK vs. FIONBIO

我们可以使用不带 usleep() 的咨询记录锁定在父子之间交替吗?

c - 套接字错误 :90:Message Too Long

javascript - 请求成功后不久出现XMLHTTPRequest Network Error的原因是什么?