写入套接字时使用 write() 而不是 send() 的性能影响
我正在使用典型的套接字 API 在 Linux 平台上用 C++ 编写网络应用程序,并且我正在研究将字节数组写入 TCP 流的两种替代方法:通过调用 write() 或通过调用发送().我知道,由于这是 Linux,套接字句柄只是一个文件描述符,因此对套接字执行 read() 和 write() 调用是有效的,但是套接字 API 还提供了 send() 和 recv() 函数来执行相同的任务.
I am working on writing a network application in C++ on the Linux platform using the typical sockets API, and I am looking at 2 alternative ways of writing a byte array to a TCP stream: either by calling write(), or by calling send(). I know that, since this is Linux, the socket handle is simply a file descriptor, and therefore it is valid to perform read() and write() calls on the socket, however the sockets API also provides the send() and recv() functions to perform the same tasks.
因此,我想知道是否有任何特别的理由选择一类函数而不是另一类――发送/接收函数是否针对网络写入/读取进行了优化,它们的性能是否更好等?或者我使用哪些功能真的很随意?read() 和 write() 在所有情况下都能正常运行吗?
I am therefore wondering if there is any particular reason to choose one class of functions over the other - are the send/recv functions optimized for network writing/reading, do they perform better, etc? Or is it really arbitrary which functions I use? Do read() and write() behave properly in all cases?
感谢您的任何见解!
推荐答案
应该没有区别.引用 man 2 send
:
There should be no difference. Quoting from man 2 send
:
send()
和 write()
之间的唯一区别是标志的存在.使用零标志参数,send()
等价于 write()
.
The only difference between
send()
andwrite()
is the presence of flags. With zero flags parameter,send()
is equivalent towrite()
.
只要你不想为 send()
指定和标记,你可以自由地使用 write()
.
So long as you don't want to specify and flags for send()
you can use write()
freely.
相关文章