使用write()而不是send()写入套接字时的性能影响 [英] Performance impact of using write() instead of send() when writing to a socket

查看:301
本文介绍了使用write()而不是send()写入套接字时的性能影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用典型的套接字API在Linux平台上使用C ++编写网络应用程序,我正在寻找两种将字节数组写入TCP流的方法:通过调用write()或者调用send()。我知道,因为这是Linux,套接字句柄只是一个文件描述符,因此它是有效的对套接字执行read()和write()调用,然而套接字API也提供send()和recv )函数来执行相同的任务。



因此,我想知道是否有任何特殊的原因选择一个类的函数在另一个 - 是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() and write() is the presence of flags. With zero flags parameter, send() is equivalent to write().

So long as you don't want to specify and flags for send() you can use write() freely.

这篇关于使用write()而不是send()写入套接字时的性能影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆