duanmou9228
2018-05-19 12:12
浏览 80
已采纳

为什么io.Copy会引发NetworkIdleTimeout错误?

please consider the runnable example below.

package main

import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "crypto/tls"
    "crypto/x509"
    "encoding/pem"
    "io"
    "log"
    "math/big"
    "time"

    quic "github.com/lucas-clemente/quic-go"
)

// var qc = &quic.Config{Versions: []quic.VersionNumber{101}}
var qc *quic.Config

// GenerateTLSConfig creates bare-bones TLS config for the server
func GenerateTLSConfig() *tls.Config {
    key, err := rsa.GenerateKey(rand.Reader, 1024)
    if err != nil {
        panic(err)
    }

    // generate a random serial number (a real cert authority would have some logic behind this)
    serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
    serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
    if err != nil {
        panic("failed to generate serial number: " + err.Error())
    }

    template := x509.Certificate{
        SerialNumber: serialNumber,
        // DNSNames:              []string{"localhost"}, // keep mint happy
        NotBefore:             time.Now(),
        NotAfter:              time.Now().Add(time.Hour * 87600), // in 10 years
        BasicConstraintsValid: true,
    }
    certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
    if err != nil {
        panic(err)
    }
    keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
    certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})

    tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
    if err != nil {
        panic(err)
    }
    return &tls.Config{
        Certificates:       []tls.Certificate{tlsCert},
        InsecureSkipVerify: true,
    }
}

func server() (quic.Listener, error) {
    return quic.ListenAddr("localhost:9001", GenerateTLSConfig(), qc)
}

func accept(l quic.Listener) (quic.Session, error) {
    return l.Accept()
}

func acceptStream(sess quic.Session) (quic.Stream, error) {
    return sess.AcceptStream()
}

func client() (quic.Session, error) {
    return quic.DialAddr("localhost:9001", GenerateTLSConfig(), qc)
}

func openStream(sess quic.Session) (quic.Stream, error) {
    return sess.OpenStreamSync()
}

func main() {
    ch := make(chan struct{})

    go func() {
        l, err := server()
        if err != nil {
            log.Fatal("failed to listen:", err)
        }

        sess, err := accept(l)
        if err != nil {
            log.Fatal("failed to accept session:", err)
        }

        s, err := acceptStream(sess)
        if err != nil {
            log.Fatal("failed to accept stream:", err)
        }

        log.Println("stream accepted")

        // b := make([]byte, len("hello, world"))
        // if _, err = s.Read(b); err != nil {
        //  log.Fatal("failed to read from stream:", err)
        // }
        // log.Println(string(b))

        buf := bytes.NewBuffer(make([]byte, len("hello, world")))
        if _, err = io.Copy(buf, s); err != nil {
            log.Fatal("failed to read from stream:", err)
        }
        log.Println(buf.String())

        close(ch)
    }()

    sess, err := client()
    if err != nil {
        log.Fatal("failed to dial:", err)
    }

    s, err := openStream(sess)
    if err != nil {
        log.Fatal("failed to open stream:", err)
    }

    log.Print("writing")
    if _, err = io.Copy(s, bytes.NewBuffer([]byte("hello, world"))); err != nil {
        log.Fatal("failed to write:", err)
    }
    log.Print("wrote")

    <-ch
}

Running this example will produce the following output:

$ go run cmd/scratch/main.go                                                                                                    (656ms)
2018/05/19 13:09:17 writing
2018/05/19 13:09:17 wrote
2018/05/19 13:09:17 stream accepted
2018/05/19 13:09:47 failed to read from stream:NetworkIdleTimeout: No recent network activity.
exit status 1

changing

buf := bytes.NewBuffer(make([]byte, len("hello, world")))
if _, err = io.Copy(buf, s); err != nil {
    log.Fatal("failed to read from stream:", err)
}
log.Println(buf.String())

to

b := make([]byte, len("hello, world"))
if _, err = s.Read(b); err != nil {
    log.Fatal("failed to read from stream:", err)
}
log.Println(string(b))

seems to fix the issue.

Question:

Why doesn't using io.Copy with a bytes.Buffer work in this case?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题