23 # include <sys/socket.h>
36 TcpConnector::TcpConnector() =
default;
38 TcpConnector::~TcpConnector() =
default;
56 if (open(new_stream) == -1) {
62 servAddr.sin_addr.s_addr = INADDR_ANY;
63 servAddr.sin_family = AF_INET;
64 servAddr.sin_port = htons(address.
getPort());
65 memset(servAddr.sin_zero,
'\0',
sizeof servAddr.sin_zero);
67 struct hostent* hostInfo = yarp::os::impl::gethostbyname(address.
getHost().c_str());
69 bcopy(hostInfo->h_addr,
reinterpret_cast<char*
>(&servAddr.sin_addr), hostInfo->h_length);
71 inet_pton(AF_INET, address.
getHost().c_str(), &servAddr.sin_addr);
85 if ((arg = fcntl(handle, F_GETFL, NULL)) < 0) {
86 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_GETFL): %d, %s", errno, strerror(errno));
90 if (fcntl(handle, F_SETFL, arg) < 0) {
91 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_SETFL): %d, %s", errno, strerror(errno));
95 res = ::connect(handle,
reinterpret_cast<sockaddr*
>(&servAddr),
sizeof(servAddr));
98 if (errno == EINPROGRESS) {
100 FD_SET(handle, &myset);
101 res = select(handle + 1,
nullptr, &myset,
nullptr, timeout);
102 if (res < 0 && errno != EINTR) {
103 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error connecting: %d, %s", errno, strerror(errno));
105 }
else if (res > 0) {
109 if (getsockopt(handle, SOL_SOCKET, SO_ERROR,
reinterpret_cast<void*
>(&valopt), &lon) < 0) {
110 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error in getsockopt(): %d, %s", errno, strerror(errno));
119 yCError(TCPCONNECTOR_POSIX,
"connect fail: Timeout in select() - Cancelling!: %d, %s", errno, strerror(errno));
123 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error connecting: %d, %s", errno, strerror(errno));
129 char buf[INET_ADDRSTRLEN];
131 "Connect [handle=%d] at %s:%d",
133 inet_ntop(AF_INET, &servAddr.sin_addr, buf, INET_ADDRSTRLEN),
139 if ((arg = fcntl(handle, F_GETFL,
nullptr)) < 0) {
140 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_GETFL): %d, %s", errno, strerror(errno));
143 arg &= (~O_NONBLOCK);
144 if (fcntl(handle, F_SETFL, arg) < 0) {
145 yCError(TCPCONNECTOR_POSIX,
"connect fail: Error fcntl(..., F_SETFL): %d, %s", errno, strerror(errno));