Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 限制到服务器的最大连接数不工作_C_Sockets_Posix - Fatal编程技术网

C 限制到服务器的最大连接数不工作

C 限制到服务器的最大连接数不工作,c,sockets,posix,C,Sockets,Posix,我已经使用C套接字编程构建了一个客户机-服务器程序,它在我的Ubuntu操作系统(运行在VMware上)上运行得非常好。我唯一的问题是API调用 虽然我已将连接限制设置为2,但我可以同时打开四个终端并连接到服务器 listen (serverFd, 2); /* Maximum pending connection length */ 客户端和服务器在同一台计算机上运行 这是代码片段 #include <signal.h> #include <sys/types.h>

我已经使用C套接字编程构建了一个客户机-服务器程序,它在我的Ubuntu操作系统(运行在VMware上)上运行得非常好。我唯一的问题是API调用

虽然我已将连接限制设置为2,但我可以同时打开四个终端并连接到服务器

 listen (serverFd, 2); /* Maximum pending connection length */
客户端和服务器在同一台计算机上运行

这是代码片段

#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>       /* for sockaddr_un struct */
#define DEFAULT_PROTOCOL   0
#define BUFFER_SIZE 1024


/* POSIX renames "Unix domain" as "local IPC."
    Not all systems define AF_LOCAL and PF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL    AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL    PF_UNIX
#endif


/****************************************************************/
main ()
{

    printf("Hello, server is starting...\n");

    // initialize data from text file into system
    readData();

    printf("Country data loaded with total of %i countries available.\n", NoOfRecordsRead);

    if(NoOfRecordsRead == 0)
    {
        printf("No valid data to serve, terminating application...\n");
        exit (-1);
    }

    int serverFd, clientFd, serverLen, clientLen;
    struct sockaddr_un serverAddress;/* Server address */
    struct sockaddr_un clientAddress; /* Client address */
    struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
    struct sockaddr* clientSockAddrPtr; /* Ptr to client address */

    /* Ignore death-of-child signals to prevent zombies */
    signal (SIGCHLD, SIG_IGN);

    serverSockAddrPtr = (struct sockaddr*) &serverAddress;
    serverLen = sizeof (serverAddress);

    clientSockAddrPtr = (struct sockaddr*) &clientAddress;
    clientLen = sizeof (clientAddress);

    /* Create a socket, bidirectional, default protocol */
    serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
    serverAddress.sun_family = AF_LOCAL; /* Set domain type */
    strcpy (serverAddress.sun_path, "country"); /* Set name */
    unlink ("country"); /* Remove file if it already exists */
    bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
    listen (serverFd, 2); /* Maximum pending connection length */

    printf("Server started.\n");

    while (1) /* Loop forever */
    {
        /* Accept a client connection */
        clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);

        if (fork () == 0) /* Create child to send recipe */
        {
                 //do something

        }

    }

}   
为什么会这样

虽然我已将连接限制设置为2

没有。您已将listen backlog设置为2。阅读有关listen命令的文档:

backlog参数定义sockfd的挂起连接队列可能增长到的最大长度。如果有连接 当队列已满时,请求到达,客户端可能会收到一个错误,指示ECONREFUNCE,或者,如果底层 协议支持重传,请求可能会被忽略,以便稍后在连接时重新尝试成功

如果要限制同时连接的数量,程序可以决定不调用accept

虽然我已将连接限制设置为2

没有。您已将listen backlog设置为2。阅读有关listen命令的文档:

backlog参数定义sockfd的挂起连接队列可能增长到的最大长度。如果有连接 当队列已满时,请求到达,客户端可能会收到一个错误,指示ECONREFUNCE,或者,如果底层 协议支持重传,请求可能会被忽略,以便稍后在连接时重新尝试成功


如果您想限制同时连接的数量,您的程序可以决定不调用accept。

+1,平台可以静默地向下或更经常地向上调整积压值。+1,平台可以静默地向下或更经常地向上调整积压值。