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

标签 sockets go epoll

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

我尝试通过 linux epoll 实现一个 http 服务器,虽然一切正常,但浏览器总是在服务器完成通过套接字发送后一直等待,直到我中断进程。我应该发送一些终止字符还是做其他终止操作?以上只是socket写http响应的代码。

最佳答案

状态行中有错字。使用

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

服务器应该执行以下操作之一来终止请求正文:

  • 用正文的长度指定 Content-Length header 。
  • 编写带有终止 block 的分 block 响应。
  • 指定 Connection: close header 并在写入响应后关闭连接。

关于sockets - golang epoll 发送消息后必须关闭套接字吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48532814/

相关文章:

c - SO_REUSEPORT 与 epoll 和多进程

c - 一个 nginx worker 进程是同时处理两个请求还是一个一个处理?

c - 绑定(bind)错误 (99) : Cannot assign requested address

c# - 将 PayPal 与控制台应用程序集成

linux - 在不伪造 RST、Linux 的情况下强制套接字断开连接

go - 作为对象的变异参数

terminal - 在 golang 的终端屏幕中央显示文本

session - cookie 和 cookiejar 有什么区别?

c++ - 你能推荐一些关于 Linux 上 Epoll 的指南吗?

c - epoll的edge triggered option的作用是什么?