单个 IP 只能支持 65535 端口到单个目标。希望client在性能测试的时候能够立即重用旧的tcp_session,即使session还处于time_wait状态。
在我的 Linux 机器上,我打开了这些开关
sysctl -w net.ipv4.tcp_timestamps=1
sysctl -w net.ipv4.tcp_tw_recycle=1
sysctl -w net.ipv4.tcp_tw_reuse=1
然后我写了下面的代码来用golang验证socket_reuse选项。在代码中,我绑定了本地端口12345。
先跑后
$go run 1.go
$netstat -nat | grep 12345
tcp 0 0 192.168.1.11:12345 111.161.3.173:80 TIME_WAIT
二次运行后
$go run 1.go
Client Connect() called error: cannot assign requested address
似乎 SO_REUSEADDR 不能工作。任何人都可以帮助解决这个问题吗?
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{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
}
}
慕田峪9158850
相关分类