英文:
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/
英文:
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/
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论