linux做为服务器系统,当socket运行高并发TCP程序时,一般会出现链接创建到必定个数后不能再创建链接的状况linux
本人在工做时,测试高并发tcp程序(GPS服务器端程序),屡次测试,发现每次链接创建到1000左右时,不再能创建tcp链接,最总上网搜索,linux系统默认ulimit为1024个访问 用户最多可开启的程序数目。通常一个端口的最高链接为2的16次方65535bash
soft nofile 65535 hard nofile 65535
session required /lib/security/pam_limits.so服务器
若是是64bit系统的话,应该为 :
session required /lib64/security/pam_limits.socookie
net.ipv4.ip_local_port_range = 1024 65535 net.core.rmem_max=16777216 net.core.wmem_max=16777216 net.ipv4.tcp_rmem=4096 87380 16777216 net.ipv4.tcp_wmem=4096 65536 16777216 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_window_scaling = 0 net.ipv4.tcp_sack = 0 net.core.netdev_max_backlog = 30000 net.ipv4.tcp_no_metrics_save=1 net.core.somaxconn = 262144 net.ipv4.tcp_syncookies = 0 net.ipv4.tcp_max_orphans = 262144 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2
/sbin/sysctl -p /etc/sysctl.conf /sbin/sysctl -w net.ipv4.route.flush=1
echo ulimit -HSn 65536 >> /etc/rc.local echo ulimit -HSn 65536 >>/root/.bash_profile ulimit -HSn 65536
经过修改,tcp能够达到20000个链接彻底没有问题网络