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

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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器