Home > Backend Development > Golang > How to Read Data of Unknown Length with Go's net.Conn.Read?

How to Read Data of Unknown Length with Go's net.Conn.Read?

DDD
Release: 2024-11-10 00:15:02
Original
642 people have browsed it

How to Read Data of Unknown Length with Go's net.Conn.Read?

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))
}
Copy after login

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())
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template