Read whole data with Golang net.Conn.Read

前端 未结 3 809
时光取名叫无心
时光取名叫无心 2020-12-25 12:27

So I\'m building a network app in Go and I\'ve seen that Conn.Read reads into a limited byte array, which I had created with make([]byte, 2048) and

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-25 13:11

    You can use the ioutil.ReadAll function:

    import (
        "fmt"
        "io/ioutil"
        "net"
    )
    
    func whois(domain, server string) ([]byte, error) {
        conn, err := net.Dial("tcp", server+":43")
        if err != nil {
            return nil, err
        }
        defer conn.Close()
    
        fmt.Fprintf(conn, "%s\r\n", domain)
        return ioutil.ReadAll(conn)
    }
    

提交回复
热议问题