Performance impact of using write() instead of send() when writing to a socket

前端 未结 3 1503
忘掉有多难
忘掉有多难 2021-01-11 11:52

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

相关标签:
3条回答
  • 2021-01-11 12:41

    recv and send allow you to specify flags, such as for out-of-band packets. If you don't need to specify the flags, read and write are perfectly adequate.

    0 讨论(0)
  • 2021-01-11 12:46

    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.

    0 讨论(0)
  • 2021-01-11 12:51

    write v.s. send for a single byte, or a single array probably won't be much different - they will soon end up the same code path (ultimately they perform the same operation). The overhead in network transmission is highly unlikely to be at that level; it will the actual TCP connection and moving the bits over wires.

    However, if you are intending to send large, multipart messages in a single go you should look at the sendmsg() syscall - this allows you to specify a list of non-contiguous arrays of data to send.

    At the end of the day the normal guidelines apply - write the application first, then benchmark to see where your bottlenecks are.

    0 讨论(0)
提交回复
热议问题