Understanding sendfile() and splice()

后端 未结 1 630
没有蜡笔的小新
没有蜡笔的小新 2020-12-30 10:52

sendfile() can be used to transmit data from a \"file\" descriptor to a \"socket\" descriptor in order to get data from machine A to machine B. Is it possible t

相关标签:
1条回答
  • 2020-12-30 11:29

    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.

    0 讨论(0)
提交回复
热议问题