Understanding sendfile() and splice()

蹲街弑〆低调 提交于 2019-11-30 07:24:59

You're correct about the limitation of sendfile for this. And yes, splice can help, but it's not trivial: splice requires that at least one of the source or target file descriptors be a pipe. So you can't directly splice from a socket to a plain file descriptor.

Conceptually, what you can do to make it work is:

  • setup your inbound socket fd and your output file fd as you would normally
  • create a pipe with pipe(2)
  • in a loop:
    • read from the socket to the write side of the pipe with splice
    • write from the read side of the pipe to the file with splice also

Repeat the last steps until all the data is read.

Zero-Copy in Linux with sendfile() and splice() has an implementation of this technique.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!