使用write()而不是send()写入套接字时的性能影响 [英] Performance impact of using write() instead of send() when writing to a socket
问题描述
因此,我想知道是否有任何特殊的原因选择一个类的函数在另一个 - 是send / recv函数优化网络写/读,他们表现更好等等?还是真的随意使用哪些功能?在所有情况下,read()和write()的行为是否正确?
感谢任何见解!
应该没有区别。引用 man 2 send :
send()
和write()
是标志的存在。使用零标志参数,send()
等效于write()
。
只要你不想为 send()
指定和标志,就可以使用 write()
。
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.
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?
Thanks for any insights!
There should be no difference. Quoting from man 2 send
:
The only difference between
send()
andwrite()
is the presence of flags. With zero flags parameter,send()
is equivalent towrite()
.
So long as you don't want to specify and flags for send()
you can use write()
freely.
这篇关于使用write()而不是send()写入套接字时的性能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!