GolangNote

Golang笔记

Golang socket 连接、发送、读取超时处理

Permalink

Golang socket 连接、发送、读取超时处理

Go: 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 转摘请注明来源

Related articles

Golang sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

Write a Comment to "Golang socket 连接、发送、读取超时处理"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 2ms