dqwh1208 2018-01-31 00:49
浏览 137
已采纳

golang epoll发送消息后必须关闭套接字吗?

go func() {
    for req := range respChan {
        content := make([]byte, 0, 1024*32)
        content = append(content, []byte("HTTP1.1 200 OK
")...)
        for k, v := range req.Response.Headers {
            content = append(content, []byte(fmt.Sprintf("%s:%s
", k, v))...)
        }
        content = append(content, []byte("
")...)
        content = append(content, req.Response.Content...)
        fmt.Println(string(content[:]))
        _, err := syscall.Write(int(req.Fd), content)
        handleEpollError(err)
    }
}()

I try to implement a http server by linux epoll, eventhing is ok but browser always keep waiting after server finish sending by socket until I interrupt the process. Should I send some terminate characters or do other terminate operation? Above is only the code writing http response by socket.

  • 写回答

1条回答 默认 最新

  • drbi19093 2018-01-31 00:55
    关注

    There's a typo on the status line. Use

    content = append(content, []byte("HTTP/1.1 200 OK
    ")...)
    

    The server should do one of the following to terminate the request body:

    • Specify the Content-Length header with the length of the body.
    • Write a chunked response with a terminating chunk.
    • Specify Connection: close header and close connection after writing response.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?