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 to get the data at the receiving end from the "socket" descriptor to a file with similar zero-copy semantics? I think sendfile()
doesn't help here because sendfile()
needs the source of data to be "page/buffer" cache. Is my understanding correct? Can splice()
help in this situation?
相关问题
- Is shmid returned by shmget() unique across proces
- how to get running process information in java?
- Error building gcc 4.8.3 from source: libstdc++.so
- Why should we check WIFEXITED after wait in order
- Null-terminated string, opening file for reading
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 directlysplice
from a socket to a plain file descriptor.Conceptually, what you can do to make it work is:
pipe(2)
splice
splice
alsoRepeat the last steps until all the data is read.
Zero-Copy in Linux with sendfile() and splice() has an implementation of this technique.