7 # error include tcp_client-windows.h instead
14 #include <sys/socket.h>
15 #include <arpa/inet.h>
18 #include <netinet/tcp.h>
54 void connect(
const std::string &host,
int port)
59 memset(&hints, 0,
sizeof(
struct addrinfo));
60 hints.ai_family = AF_INET;
61 hints.ai_socktype = SOCK_STREAM;
62 hints.ai_flags = AI_NUMERICSERV;
63 hints.ai_protocol = 0;
66 struct addrinfo *addrinfo_result;
67 auto rv = ::getaddrinfo(host.c_str(), port_str.c_str(), &hints, &addrinfo_result);
75 for (
auto *rp = addrinfo_result; rp !=
nullptr; rp = rp->ai_next)
77 #if defined(SOCK_CLOEXEC)
78 const int flags = SOCK_CLOEXEC;
82 socket_ = ::socket(rp->ai_family, rp->ai_socktype |
flags, rp->ai_protocol);
97 ::freeaddrinfo(addrinfo_result);
105 ::setsockopt(
socket_, IPPROTO_TCP, TCP_NODELAY,
reinterpret_cast<char *
>(&enable_flag),
sizeof(enable_flag));
108 #if defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
109 ::setsockopt(
socket_, SOL_SOCKET, SO_NOSIGPIPE,
reinterpret_cast<char *
>(&enable_flag),
sizeof(enable_flag));
112 #if !defined(SO_NOSIGPIPE) && !defined(MSG_NOSIGNAL)
113 # error "tcp_sink would raise SIGPIPE since niether SO_NOSIGPIPE nor MSG_NOSIGNAL are available"
121 size_t bytes_sent = 0;
122 while (bytes_sent < n_bytes)
124 #if defined(MSG_NOSIGNAL)
125 const int send_flags = MSG_NOSIGNAL;
127 const int send_flags = 0;
129 auto write_result =
::send(
socket_,
data + bytes_sent, n_bytes - bytes_sent, send_flags);
130 if (write_result < 0)
136 if (write_result == 0)
140 bytes_sent +=
static_cast<size_t>(write_result);