Boost logo

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