获取linux socket最大连接数

socket连接数的理论值应该和一个进程所能打开的最大文件描述符数相等,所以编写简单程序在Fedora Core Linux下测试得:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        long val;

        if((val = sysconf(_SC_OPEN_MAX)) < 0) {
                printf("Get the maximum number of files that a process can have open at any time failed/n");
                exit(1);
        } else {
                printf("The maximum number of files that a process can have open at any time is %ld/n", val);
        }

        return 0;
}

结果:The maximum number of files that a process can have open at any time is 1024


修改linux socket最大连接数

有两种方法:
1、ulimit -n 5000    
    把允许最大打开的描述符修改为5000,只对当前终端打开的应用程序有效。

2、修改/etc/security/limits.conf

    /etc/security/limits.conf
    #
    #Each line describes a limit for a user in the form:
    #
    #<domain>        <type>  <item>  <value>
    #
    #Where:
    #<domain> can be:
    #        - an user name
    #        - a group name, with @group syntax
    #        - the wildcard *, for default entry
    #        - the wildcard %, can be also used with %group syntax,
    #                 for maxlogin limit
    #
    #<type> can have the two values:
    #        - "soft" for enforcing the soft limits
    #        - "hard" for enforcing hard limits
    #
    #<item> can be one of the following:
    #        - core - limits the core file size (KB)
    #        - data - max data size (KB)
    #        - fsize - maximum filesize (KB)
    #        - memlock - max locked-in-memory address space (KB)
    #        - nofile - max number of open files
    #        - rss - max resident set size (KB)
    #        - stack - max stack size (KB)
    #        - cpu - max CPU time (MIN)
    #        - nproc - max number of processes
    #        - as - address space limit (KB)
    #        - maxlogins - max number of logins for this user
    #        - maxsyslogins - max number of logins on the system
    #        - priority - the priority to run user process with
    #        - locks - max number of file locks the user can hold
    #        - sigpending - max number of pending signals
    #        - msgqueue - max memory used by POSIX message queues (bytes)
    #        - nice - max nice priority allowed to raise to values: [-20, 19]
    #        - rtprio - max realtime priority
    #
    #<domain>      <type>  <item>         <value>
    #

    #*               soft    core            0

    在该文件中添加以下两行:
    
    *      soft    nofile     5000
    *      hard    nofile     20000


    解释:

    *      表示该配置对所有用户均有效
    soft     表示软连接数
    hard     表示硬连接数
    nofile    配置是针对描述符的

Logo

更多推荐