linux做爲服務器系統,當運行高併發TCP程序時,一般會出現鏈接創建到必定個數後不能再創建鏈接的狀況linux
本人在工做時,測試高併發tcp程序(GPS服務器端程序),屢次測試,發現每次鏈接創建到3800左右時,不再能創建tcp鏈接,最總上網搜索,參考:http://blog.csdn.net/guowake/article/details/6615728解決了鏈接限制的問題,此處記錄下來,方便往後工做繼續使用,雖然參考博文中內容有點多,文中所描述的內容也不怎麼明白,但總結下來,按以下幾步操做便可(我映像中,按此方法設置後,測試創建鏈接8000多時也未出現什麼錯誤) :bash
* soft nofile 20000服務器
* hard nofile 20000cookie
session required /lib/security/pam_limits.so網絡
若是是64bit系統的話,應該爲 :
session required /lib64/security/pam_limits.sosession
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