Go TCP Read is Non-Blocking: Addressing Incomplete Data Reception
In Go, TCP reads are non-blocking, meaning they return immediately with any available data, even if it's less than expected. This behavior differs from C's blocking reads, which wait until the required amount of data is received.
Reason for Non-Blocking Read
TCP operates as a stream of bytes, which may be fragmented during transmission. Therefore, it's impossible to determine the end of a message based solely on the number of bytes received. This requires custom delimiters or other mechanisms to determine message boundaries.
Solution to Incomplete Data
To read a specific number of bytes, use io.ReadAtLeast or io.ReadFull. For arbitrary conditions, loop on the Read call until there's no error or the specified condition is met.
Example:
package main
import (
"fmt"
"net"
"time"
)
func main() {
conn, _ := net.Dial("tcp", "127.0.0.1:4243")
// Set a deadline to prevent hanging reads
_ = conn.SetReadDeadline(time.Now().Add(10 * time.Second))
// Loop to read until a newline is encountered
for {
buf := make([]byte, 512)
n, err := conn.Read(buf)
if err != nil {
fmt.Println(err)
break
}
if n > 0 {
str := string(buf[:n])
if str[len(str)-1] == '\n' {
fmt.Println(str)
break
}
}
}
}
Other Considerations
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3