详情参考:https://www.ibm.com/developerworks/linux/library/j-zerocopy/
一、传统发送示意图
Listing 1. Copying bytes from a file to a socket
File.read(fileDesc, buf, len); Socket.send(socket, buf, len);
二、zero-copy方法
Listing 2. The transferTo()
method
public void transferTo(long position, long count, WritableByteChannel target);
Listing 3. The sendfile()
system call
#include <sys/socket.h> ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
如果网卡和操作系统支持gather操作,还可以进一步优化,如下图