`
ywChen
  • 浏览: 118120 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

java.net.BindException: Address already in use: connect的问题

 
阅读更多

    大概原因是短时间内new socket操作很多,而socket.close()操作并不能立即释放绑定的端口,而是把端口设置为TIME_WAIT状态,过段时间(默认240s)才释放,(用netstat -na可以看到),最后系统资源耗尽(windows上是耗尽了pool of ephemeral ports ,这段区间在1024-5000之间; )

    避免出现这一问题的方法有两个,一个是调高你的web服务器的最大连接线程数,调到1024,2048都还凑合,以resin为例,修改resin.conf中的thread-pool.thread_max,如果你采用apache连resin的架构,别忘了再调整apache; 另一个是修改运行web服务器的机器的操作系统网络配置,把time wait的时间调低一些,比如30s。

    在Linux上,查看有关的选项, 
[xxx@xxx~]$ /sbin/sysctl -a|grep net.ipv4.tcp_tw 
net.ipv4.tcp_tw_reuse = 0 
net.ipv4.tcp_tw_recycle = 0 
[xxx@xxx~]$vi /etc/sysctl.conf,修改 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_tw_recycle = 1 
[xxx@xxx~]$sysctl -p,使内核参数生效

另:

1. 您试图从大于 5000 的 TCP 端口连接时收到错误 WSAENOBUFS (10055)

默认最大数目的临时的 TCP 端口为"适用于"部分中包括的产品中的 5000。这些产品中添加了一个新参数。若要增加最大数量的临时端口,请按照下列步骤操作:

  1. 启动注册表编辑器。
  2. 在注册表中,找到下面的子项,然后单击 参数

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

  3. 在 编辑 菜单上单击 新建,然后添加以下注册表项:

    值名称: MaxUserPort
    值类型: DWORD
    值数据: 65534
    有效范围: 5000-65534 (十进制)
    默认值: 0x1388 (5000 十进制)
    说明: 此参数控制程序从系统请求任何可用的用户的端口时所用的最大端口数。 通常,1024年的值 (含) 之间的 5000 之间分配暂时 (短期) 的端口。发布的安全公告 ms08-037 Windows Server 2003 的行为已更改为更接近后与 Windows Server 2008 和 Windows Vista。Microsoft 安全公告 ms08-037 有关的详细信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:

    951746 (http://support.microsoft.com/kb/951746/ ) ms08-037: 说明 Windows Server 2008 中,Windows Server 2003,和 Windows 2000 服务器 (DNS 服务器端) 的 DNS 的安全更新: 2008 年 7 月 8

    951748 (http://support.microsoft.com/kb/951748/ ) ms08-037: 说明在 Windows Server 2003 中、 Windows XP 中和 Windows 2000 Server (客户端) 中的 DNS 的安全更新: 2008 年 7 月 8

    953230 (http://support.microsoft.com/kb/953230/ ) ms08-037: 在 DNS 中的漏洞可能允许欺骗

  4. 退出注册表编辑器,然后重新启动计算机。

注意一个附加的 TCPTimedWaitDelay 注册表参数确定一个闭合的长端口一直等待,直到已关闭的端口可重复使用。


参考:http://support.microsoft.com/kb/q196271/

2.安装linux后的内核调优
kernel.shmall = 268435456
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 5000 65000
net.ipv4.tcp_mem = 786432 1048576 1572864
net.core.wmem_max = 873200
net.core.rmem_max = 873200
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.core.somaxconn = 256
net.core.netdev_max_backlog = 1000
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_keepalive_time = 500
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.conf.lo.arp_ignore = 0
net.ipv4.conf.lo.arp_announce = 0
net.ipv4.conf.all.arp_ignore = 0
net.ipv4.conf.all.arp_announce = 0

几个解释:
net.ipv4.tcp_syncookies = 1
#表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1
#表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1
#表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout = 30
#表示如果套接字由本端要求关闭,这个参数决定了它保持在FIN-WAIT-2状态的时间。
net.ipv4.tcp_keepalive_time = 1200 
#表示当keepalive起用的时候,TCP发送keepalive消息的频度。缺省是2小时,改为20分钟。
net.ipv4.ip_local_port_range = 1024    65000 
#表示用于向外连接的端口范围。缺省情况下很小:32768到61000,改为1024到65000。
net.ipv4.tcp_max_tw_buckets = 5000
#表示系统同时保持TIME_WAIT套接字的最大数量,如果超过这个数字,
#TIME_WAIT套接字将立刻被清除并打印警告信息。默认为180000,改为5000。
#对于Apache、Nginx等服务器,上几行的参数可以很好地减少TIME_WAIT套接字数量,
#但是对于Squid,效果却不大。此项参数可以控制TIME_WAIT套接字的最大数量,避免Squid服务器被大量的TIME_WAIT套接字拖死。

 

 

 

本人开了个充值淘宝网店。有需要的朋友请访问的店铺并拍下所充值的话费,

本店已加入消费保障服务计划,货源来源于淘宝充值平台,安全可靠便捷,

支付过后立即到账

http://xiaowen168.taobao.com

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics