Go Socket Server出现EOF错误?

huangapple go评论77阅读模式
英文:

Go Socket Server got EOF error?

问题

我的代码:

var connMax int = 0

func CheckErr(err error) {
    if err != nil {
        fmt.Fprintf(os.Stderr, "发生错误:%s\n", 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\r\n"))
}

func Run() {
    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 http://127.0.0.1:8080/

一旦完成,就会出现EOF错误,所以进程退出:
Go Socket Server出现EOF错误?

英文:

My codes:

    var connMax int = 0

    func CheckErr(err error) {
        if err != nil {
            fmt.Fprintf(os.Stderr, "Error occured: %s\n", 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\r\n"))

    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:
Go Socket Server出现EOF错误?

答案1

得分: 0

io.EOF表示连接被另一端关闭。我猜这是因为ab达到了打开文件描述符的限制,所以连接被关闭了。使用ulimit -a来检查它。如果它小于10000,使用ulimit -n 65536来更改它。这样可以允许超过10000个连接。我随机选择了65536,你需要root权限来进行这个配置。

英文:

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.

huangapple
  • 本文由 发表于 2016年4月9日 13:45:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/36513215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定