获取和修改linux socket最大连接数

获取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    配置是针对描述符的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值