单IP只能发起65535个连接,我希望性能测试的时候,连接即使是timewait也能复用。
然后就写了一个如下的小代码来验证,socket中的reuse 选项功能
```
package main
import (
"fmt"
. "syscall"
)
func main() {
var (
clientsock int
serveraddr SockaddrInet4
err error
)
if clientsock, err = Socket(AF_INET, SOCK_STREAM, IPPROTO_IP); err != nil {
fmt.Println("Client Socket() called error:", err.Error())
return
}
SetsockoptInt(clientsock, SOL_SOCKET, SO_REUSEADDR, 1)
defer Shutdown(clientsock, SHUT_RDWR)
//serveraddr.Addr = [4]byte{58, 217, 200, 15}
serveraddr.Addr = [4]byte{111, 161, 3, 173}
serveraddr.Port = 80
err = Bind(clientsock, &SockaddrInet4{
Port: 12345,
})
if err = Connect(clientsock, &serveraddr); err != nil {
fmt.Println("Client Connect() called error:", err.Error())
return
}
}
```
然后,我在linux机器上 已经开启了以下的选项
```
sysctl -w net.ipv4.tcp_timestamps=1
sysctl -w net.ipv4.tcp_tw_recycle=1
sysctl -w net.ipv4.tcp_tw_reuse=1
```
可是当执行代码的时候,先运行一遍,以上述代码为例,其实本地绑定了12345端口, 当执行完一遍之后
```
$go run 1.go
$netstat -nat | grep 12345
tcp 0 0 192.168.1.11:12345 111.161.3.173:80 TIME_WAIT
```
将发现 12345 已经是timewait状态
此时再运行一遍代码,将有如下报错
```
$go run 1.go
Client Connect() called error: cannot assign requested address
```
我想知道如何解决?
更多评论