【问题标题】:Server can accept n clients only服务器只能接受 n 个客户端
【发布时间】:2014-11-14 10:55:54
【问题描述】:

我正在使用 C 语言编写服务器代码,该代码必须只接受少量客户端,如果有额外的客户端到达,服务器将让该客户端等待,直到其中一个旧客户端终止。

例如 (服务器只能接受 10 个客户端,如果有新客户端到达,服务器将让该客户端等待,直到 10 个客户端之一终止,然后才能为他提供服务。

我知道我必须在listen() 函数之后和accept() 之前使用signal() 函数并创建一个计算客户端数量的值,但我不知道如何正确使用它。

谁能给我一个提示或简单的例子。

谢谢你,,,,,,,

【问题讨论】:

  • 你有没有尝试过...?
  • 查找“多线程”,尤其是 pthreads
  • put that client to wait ... 怎么样? wait 在这里具体指的是什么?
  • @SouravGhosh 服务器将阻止该客户端
  • 又是@Ali,怎么block?拒绝?那么你可以在listen() 中使用backlog 来实现这一点。见man page

标签: c client-server server signal-handling


【解决方案1】:

没有必要使用signal()。示例:

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

main()
{
    int s = socket(PF_INET, SOCK_STREAM, 0);    // server socket
    if (s            == -1) return perror("socket"), 1;
    if (listen(s, 0) == -1) return perror("listen"), 1;
    const int n = 10;   // number of clients allowed to be served
    fd_set fds, rfds;
    FD_ZERO(&fds);
    FD_SET(s, &fds);    // initially, set of sockets holds server
    int nfds = s+1, fd, clients = 0;
    while (rfds = fds, select(nfds, &rfds, NULL, NULL, NULL) > 0)
        for (fd = 0; fd < nfds; ++fd)
            if (FD_ISSET(fd, &rfds))    // see which sockets of set are ready
                if (fd == s)            // is it the server socket?
                {   // yes, so it is a client's connection request
                    printf("new client request ");
                    struct sockaddr_in sa = { AF_INET, 0, INADDR_ANY };
                    socklen_t sal = sizeof sa;
                    int c = accept(s, (struct sockaddr *)&sa, &sal);
                    if (c == -1) return perror("accept"), 1;
                    FD_SET(c, &fds); if (nfds <= c) nfds = c+1; // add client
                    printf("accepted (fd=%d) # clients now %d\n", c, ++clients);
                    if (clients == n)   // allowed number hit?
                        puts("Further client requests will be ignored."),
                        FD_CLR(s, &fds);    // don't watch server now
                }
                else
                {   // this is a client's message or termination
                    printf("client fd=%d: ", fd);
                    char buf[BUFSIZ];
                    size_t count = read(fd, buf, sizeof buf);
                    if (count > 0) fwrite(buf, 1, count, stdout);
                    else
                    {   // no more data from client, so close the connection
                        close(fd);
                        FD_CLR(fd, &fds);   // remove client from watch set
                        printf("closed, # clients now %d\n", --clients);
                        if (clients == n-1) // went just below allowed number?
                            FD_SET(s, &fds),    // watch server again
                            puts("New client requests will be accepted again.");
                    }
                }
    return perror("select"), 1;
}

注意:这个程序绑定到一个随机的空闲端口,你可以用 e 找到它。 G。 netstat -tlp。当然你也可以bind()到特定的地址和端口。在任何情况下,您都可以使用 e 对其进行测试。 G。 nc hostname port.

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2018-04-16
    • 2021-11-01
    • 2015-06-09
    • 2015-02-14
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    相关资源
    最近更新 更多