go使用tcp进行通信的keepalive问题

我现在用SocketTool在192.168.37.1上开启一个tcp服务器,这个服务器接收到连接请求后什么都不做
SocketTool:
在192.168.37.131上使用go语言编写客户端去连接192.168.37.1上的服务器并循环读取数据,由于服务器没发送数据,所以不会收到数据,我使用wireshark进行抓包发现192.168.37.131上的客户端一直在发送tcp keepalive心跳包给192.168.37.1的服务器,我在网上查询tcp的keepalive是默认不开启的,而且网上说的是服务器给客户端发送tcp keepalive包,并且时间也是要2小时以后,我不明白为什么我这儿是客户端给服务器发送keepalive包,并且时间也是十几秒后就发送了
go客户端代码:

阅读 3.2k
2 个回答

怀疑是client使能了keepalive

请贴出client代码

你翻一下net.Dial的源码就知道了, 源码的注释写的很清楚了, 如果没有设置KeepAlive, 默认就是每隔15s发一次心跳, 如果KeepAlive设置为负数,就不会发心跳了

// For Unix networks, the address must be a file system path.
func Dial(network, address string) (Conn, error) {
    var d Dialer
    return d.Dial(network, address)
}

Dialer 的结构(这里省略掉了其他字段):

type Dialer struct {
    // KeepAlive specifies the interval between keep-alive
    // probes for an active network connection.
    // If zero, keep-alive probes are sent with a default value
    // (currently 15 seconds), if supported by the protocol and operating
    // system. Network protocols or operating systems that do
    // not support keep-alives ignore this field.
    // If negative, keep-alive probes are disabled.
    KeepAlive time.Duration
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题