dqsp60748
2011-10-29 13:02
浏览 415
已采纳

转:从套接字读取时发生意外的EOF

I have the following simple golang program to download Google's privacy policy. Unfortunately it always crashes with the error unexpected EOF after reading 6861 bytes, even though the document is much longer. Why?

package main

import "net"
import "fmt"
import "io"
import "os"

func die(msg string, s os.Error) {
    fmt.Printf("%s crashed: %v
", msg, s)
    os.Exit(1)
}

func main() {
    fd, err := net.Dial("tcp", "google.com:80")
    if err != nil { die("dial", err) }

    req := []byte("GET /intl/en/privacy/ HTTP/1.0
Host: www.google.com

")
    _, err = fd.Write(req)
    if err != nil { die("dial write", err) }

    buf := make([]byte, 1024)
    nr := 1

    for nr > 0 {
        nr, err = io.ReadFull(fd, buf)
        if err != nil { die("dial read", err) }
        fmt.Printf("read %d
", nr)
    }
}

outputs:

read 1024
read 1024
read 1024
read 1024
read 1024
read 1024
dial read crashed: unexpected EOF
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanfen9983 2011-10-30 09:01
    已采纳

    Function io.ReadFull(fd, buf) should be used only when you know that fd can feed at least len(buf) bytes.

    Instead, try the following:

    var buf bytes.Buffer
    
    nr, err := io.Copy(&buf, fd)
    if err != nil {
        die("dial read", err)
    }
    

    If copying to a file, use an os.File instead of bytes.Buffer.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongmaopan5738 2011-10-29 13:16

    The document is not much longer. It's only 412 bytes longer. Sine ReadFull cannot read all 1024 bytes it returns unexpected EOF.

    评论
    解决 无用
    打赏 举报
  • drydaenth257216154 2013-08-14 16:52

    You can also get the size of the file from the HTTP headers, and read to that length on your socket. Look for Content-Length response header.

    You should always know the size of what you are moving with IO, when it's a file and possible, and limit your read to the expected size. This will avoid a lot of issues. pseudocode:

    $size = HTTP_RESPONSE['Content-Length'];
    while(reading to $size)
    {
        do something with chunk;
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题