Reading Data of Unknown Length with Go's net.Conn.Read
When working with network applications in Go, the Conn.Read function provides a means to retrieve data from the connected endpoint. However, it operates with a limited byte array, which may not always suffice when the content's length is unknown.
Question: How can this challenge be addressed to precisely read the entire data available?
Answer: The choice of approach depends on the application's specific requirements and the nature of the data. Here are some potential solutions:
Using bufio
The bufio package offers several methods that facilitate data buffering and can be employed to read until the end of file (EOF). For instance, the following code demonstrates reading all data from a socket connection:
func main() { conn, err := net.Dial("tcp", "google.com:80") if err != nil { fmt.Println("dial error:", err) return } defer conn.Close() fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") reader := bufio.NewReader(conn) buf, err := ioutil.ReadAll(reader) if err != nil { if err != io.EOF { fmt.Println("read error:", err) } return } fmt.Println("total size:", len(buf)) }
Using bytes.Buffer
Consider using the bytes.Buffer for applications requiring a buffer of arbitrary size. The following modified example demonstrates this approach:
func main() { conn, err := net.Dial("tcp", "google.com:80") if err != nil { fmt.Println("dial error:", err) return } defer conn.Close() fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n") var buf bytes.Buffer io.Copy(&buf, conn) fmt.Println("total size:", buf.Len()) }
Note: These solutions assume the connection remains active and data continues to be received until the application reads all of it or reaches the EOF. If the connection terminates prematurely, the size of the data received may be incomplete.
The above is the detailed content of How to Read Data of Unknown Length with Go's net.Conn.Read?. For more information, please follow other related articles on the PHP Chinese website!