Golang socket 连接、发送、读取超时处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
timeOut := 2
// 连接超时
conn, err := net.DialTimeout("tcp", "118.196.43.156:49002", timeOut*time.Second)
// 发送超时
conn.SetWriteDeadline(time.Now().Add(timeOut * time.Second))
// 读取超时
conn.SetReadDeadline(time.Now().Add(timeOut * time.Second))
conn.Write(msg)
reply := make([]byte, 8192)
conn.Read(reply)
fmt.Println("srv reply:" + string(reply))
本文网址: https://golangnote.com/topic/146.html 转摘请注明来源