希望这能帮助你^_^
for {
// set SetReadDeadline
err := conn.SetReadDeadline(time.Now().Add(5 * time.Second))
if err != nil {
log.Println("SetReadDeadline failed:", err)
// do something else, for example create new conn
return
}
recvBuf := make([]byte, 1024)
n, err = conn.Read(recvBuf[:]) // recv data
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Println("read timeout:", err)
// time out
} else {
log.Println("read error:", err)
// some error else, do something else, for example create new conn
}
}
}