douxun2018
2016-04-09 05:45
浏览 144
已采纳

去套接字服务器出现EOF错误?

My codes:

    var connMax int = 0

    func CheckErr(err error) {
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error occured: %s
", err)
            os.Exit(1)
        }
    }

    func handler(conn net.Conn) {
        defer conn.Close()

        var buf [512]byte
        n, err := conn.Read(buf[0:])
        CheckErr(err)

        connMax += 1
        fmt.Println(connMax)

        result := bytes.NewBuffer(nil)
        result.Write(buf[0:n])

        fmt.Println(string(result.Bytes()))

        conn.Write([]byte("HTTP/1.1 201 OK
"))

    func Run() {
        ln, err := net.Listen("tcp", ":8080")
        CheckErr(err)
        for {
            conn, err := ln.Accept()
            CheckErr(err)
            go handler(conn)
        }
    }

I tried to testing server with command ab of package apache2-utils. ab -c 1500 -n 10000 http://127.0.0.1:8080/

once that finished, EOF error occured, so process exit: enter image description here

图片转代码服务由CSDN问答提供 功能建议

我的代码:

  var connMax int = 0 
  
 func CheckErr(错误错误){
,如果错误!= nil {
 fmt.Fprintf(os.Stderr,“发生错误:%s 
”,错误)
 os.Exit(1)
}  
} 
 
 func handler(conn net.Conn){
延迟conn.Close()
 
 var buf [512] byte 
n,err:= conn.Read(buf [0:])  
 CheckErr(err)
 
 connMax + = 1 
 fmt.Println(connMax)
 
结果:=字节.NewBuffer(nil)
结果.Write(buf [0:n])
  
 fmt.Println(string(result.Bytes()))
 
 conn.Write([] byte(“ HTTP / 1.1 201 OK 
 
”))
 
 func Run(){\  n ln,err:= net.Listen(“ tcp”,“:8080”)
 CheckErr(err)
 for {
 conn,err:= ln.Accept()
 CheckErr(err)
 go  handler(conn)
} 
} 
   
 
 

我尝试使用apache2-utils软件包的命令 ab 测试服务器。 ab -c 1500 -n 10000 htt p://127.0.0.1:8080 /

完成后,发生EOF错误,因此进程退出: ”在此处输入图片描述“

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

1条回答 默认 最新

  • doujiabing1228 2016-04-09 06:42
    最佳回答

    io.EOF indicates that the connection is closed by the other end. I guess it's because ab reaches the limitation of open file descriptors, so the connection is closed. Use ulimit -a to check it. If it's less than 10000, use ulimit -n 65536 to change it. This is to allow more than 10000 connections. I choose 65536 randomly, and you need root privilege to do this configure.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题