22 #include <arpa/inet.h> 28 timeval
initTimeout(
const unsigned int& secs,
const unsigned int& usecs)
39 std::memset((
char*)&ret, 0,
sizeof(ret));
40 ret.sin_family = AF_INET;
41 ret.sin_port = htons((
short unsigned int)port);
42 ret.sin_addr.s_addr = inet_addr(ip);
48 int file_descr_flags{ fcntl(sockfd, F_GETFL, NULL) };
49 file_descr_flags |= O_NONBLOCK;
50 fcntl(sockfd, F_SETFL, file_descr_flags);
57 FD_SET(sockfd, &writeset);
59 const int socket_ready_for_writing{ select(sockfd + 1,
nullptr, &writeset,
nullptr, &timeout_in_s) };
60 return socket_ready_for_writing > 0;
66 socklen_t optlen{
sizeof(optval) };
67 const int read_pending_errors_failed{ getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (
void*)&optval, &optlen) };
68 return !(read_pending_errors_failed == 0) || !(optval == 0);
73 const int sockfd{ socket(AF_INET, SOCK_STREAM, 0) };
77 connect(sockfd, (
const sockaddr*)&serv_addr,
sizeof(serv_addr));
83 std::this_thread::sleep_for(std::chrono::duration<double>(1));
timeval initTimeout(const unsigned int &secs, const unsigned int &usecs)
bool checkIPConnection(const char *ip, const unsigned int &port)
Test the ip connection by connecting to the modbus server.
bool hasSocketPendingErrors(const int &sockfd)
bool isSocketReadyForWriteOp(const int &sockfd)
sockaddr_in initSockAddrIn(const char *ip, const unsigned int &port)
void setConnectionToNonBlocking(const int &sockfd)