C, sendfile() and send() difference?

后端 未结 3 2024
孤独总比滥情好
孤独总比滥情好 2021-01-31 11:07

sendfile() copies data between two file descripters within kernel space. Somewhere I saw if you are writing a web server in C in linux you should use send() and recv() instead o

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 11:26

    As you have pointed out, the only difference is the flags. send/recv are for networking, whereas read/write are general I/O functions for any file descriptor. send is only useful vs write when you want to use a flag, since the flags are all network related, it doesn't make sense to call send on a non-network file descriptor (nor am I sure whether it's even valid).

    Also you should note:

    The in_fd argument must correspond to a file which supports mmap(2)-like operations (i.e., it cannot be a socket).

    Which means you can't copy from a socket (you can copy to a socket and prior to 2.6.33 you must copy to a socket).

提交回复
热议问题