如题所示错误并不一定是代码出问题了

最近在用golang抓取bitcoin块的数据,程序编译后运行良好。

运行一段时间后报错bind: An operation on a socket could not be performed because the system lacked sufficient buffer sp

上网搜查后确定问题源:代码连接端口的频次超出windows默认最大值

当然其中牵扯最大连接数量,起始中止端口号,释放连接资源时间(windows10默认120s)

 

查看端口tcp连接数量:netstat -an |find /c ":8332"

 

一共两步

 

第一步

首先设置的是windows的服务,不知道问题在哪,我设置完后重启也不起作用

打开注册表 运行->regedit 

HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters目录下

增加一个MaxUserPort(默认值是5000,端口范围是1025至5000)MaxUserPort设置为65534

增加一个TcpTimedWaitDelay(表示一个关闭后的端口等待多久之后可以重新使用,默认值为120)更改为30

TCP的KeepLive功能,可以让操作系统替我们自动清理掉CLOSE_WAIT的连接。
但是KeepLive在Windows操作系统下默认是7200秒,也就是2个小时才清理一次。往往满足不了要求。可以调小该数值。
Windows下的调整方法为
HKEY_LOCAL_MACHINE/CurrentControlSet/Services/Tcpip/Parameters下的以下三个参数: 
KeepAliveInterval,设置其值为1000   www.2cto.com  
KeepAliveTime,设置其值为300000(单位为毫秒,300000代表5分钟) 
TcpMaxDataRetransmissions,设置其值为5

bind: An operation on a socket could not be performed because the system lacked sufficient buffer sp

第二步

管理员权限下命令行依次键入以下
netsh int ipv4 set dynamicport tcp start = 10000 num = 500000 
netsh int ipv4 set dynamicport udp start = 10000 num = 500000 
netsh int ipv6 set dynamicport tcp start = 10000 num = 500000 
netsh int ipv6 set dynamicport udp start = 10000 num = 500000 
 

设置完成后可以愉快的运行代码了

相关文章: