|Prepare a tcp server listening on [::]:23380, run the following code:
#include
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
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?
|