|
Boost Users : |
From: accelerator0099 (accelerator0099_at_[hidden])
Date: 2024-04-27 03:04:07
|Prepare a tcp server listening on [::]:23380, run the following code:
#include <boost/asio.hpp>
namespace io = boost::asio;
using boost::system::system_error;
int main() {
   try {
       io::io_context ctx;
       using io::ip::tcp;
       using io::buffer;
       tcp::socket s1(ctx);
       s1.connect(tcp::endpoint(io::ip::address_v4::loopback(), 23380));
       for (size_t i{}; i != 10; ++i)
           io::write(s1, buffer("123", 3));
   } catch (system_error& e) {
       return 1;
   }
}
strace says there are 10 syscalls:
connect(6, {sa_family=AF_INET, sin_port=htons(23380),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
sendto(6, "123", 3, MSG_NOSIGNAL, NULL, 0) = 3
If I use boost::asio::ip::tcp::iostream:
#include <boost/asio.hpp>
namespace io = boost::asio;
using boost::system::system_error;
int main() {
   try {
       io::io_context ctx;
       using io::ip::tcp;
       using io::buffer;
       tcp::iostream s1{ tcp::socket(ctx) };
       auto& nl = s1.socket();
       nl.connect(tcp::endpoint(io::ip::address_v4::loopback(), 23380));
       for (size_t i{}; i != 10; ++i)
           s1.write("123", 3);
       s1.flush();
   } catch (system_error& e) {
       return 1;
   }
}
It does not make sense, still 10 syscalls:
connect(6, {sa_family=AF_INET, sin_port=htons(23380),
sin_addr=inet_addr("127.0.0.1")}, 16) = 0
ioctl(6, FIONBIO, [1])Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â = 0
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
sendmsg(6, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="123",
iov_len=3}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, MSG_NOSIGNAL) = 3
Is there a stream in asio that could buffer the multiple and small
pieces of data into one syscall?
|
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net