Skip to content

Instantly share code, notes, and snippets.

@russweas
Last active March 11, 2021 20:38
Show Gist options
  • Save russweas/eba9c09ed960430f3ba57f43031b64a5 to your computer and use it in GitHub Desktop.
Save russweas/eba9c09ed960430f3ba57f43031b64a5 to your computer and use it in GitHub Desktop.
conn.go
conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
edgeData, err := bufio.NewReader(conn).ReadString('\n') // Wait for next piece of data
if err == io.EOF {
return
}
// if err == ??? { // This is a timeout }
if err != nil {
log.Println(err)
break
}
// Outputs 2021/03/11 00:19:17 read tcp 127.0.0.1:6000->127.0.0.1:34624: i/o timeout
// SOLUTION
conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
edgeData, err := bufio.NewReader(conn).ReadString('\n') // Wait for next piece of data
if err == io.EOF {
log.Println(err)
fmt.Println("The client closed the connection")
break
} else if os.IsTimeout(err) {
fmt.Println("The connection timed out")
break
} else if err != nil {
log.Println(err)
break
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment