diff --git a/simple_message/include/simple_message/socket/simple_socket.h b/simple_message/include/simple_message/socket/simple_socket.h index 6151754d..aaa271f1 100644 --- a/simple_message/include/simple_message/socket/simple_socket.h +++ b/simple_message/include/simple_message/socket/simple_socket.h @@ -69,6 +69,7 @@ #endif #define INET_ADDR(str) inet_addr(str) #define SOCKLEN_T socklen_t +#define GETHOSTBYNAME(str) gethostbyname(str) #endif @@ -110,6 +111,7 @@ extern "C" STATUS setsockopt ( /* remove "extern C", if you're using C instead #define HTONS(num) mpHtons(num) #define INET_ADDR(str) mpInetAddr(str) #define SOCKLEN_T unsigned int +#define GETHOSTBYNAME(str) NULL #endif diff --git a/simple_message/include/simple_message/socket/tcp_socket.h b/simple_message/include/simple_message/socket/tcp_socket.h index 08374bf7..d2d24b99 100644 --- a/simple_message/include/simple_message/socket/tcp_socket.h +++ b/simple_message/include/simple_message/socket/tcp_socket.h @@ -42,7 +42,7 @@ #ifdef LINUXSOCKETS #include "sys/socket.h" -#include "netdb.h" /* gethostbyname */ +#include "netdb.h" #include "arpa/inet.h" #include "string.h" #include "unistd.h" diff --git a/simple_message/src/socket/tcp_client.cpp b/simple_message/src/socket/tcp_client.cpp index 7eaf8bdd..fb2e21e9 100644 --- a/simple_message/src/socket/tcp_client.cpp +++ b/simple_message/src/socket/tcp_client.cpp @@ -72,14 +72,18 @@ bool TcpClient::init(char *buff, int port_num) LOG_WARN("Failed to set no socket delay, sending data can be delayed by up to 250ms"); } - // Initialize address data structure memset(&this->sockaddr_, 0, sizeof(this->sockaddr_)); this->sockaddr_.sin_family = AF_INET; - if (NULL != (ent = gethostbyname(buff))) { + + // Check for 'buff' as hostname, and use that, otherwise assume IP address + if (NULL != (ent = GETHOSTBYNAME(buff))) + { in_a = (struct in_addr *) ent->h_addr_list[0]; this->sockaddr_.sin_addr.s_addr = in_a->s_addr; - } else { + } + else + { this->sockaddr_.sin_addr.s_addr = INET_ADDR(buff); } this->sockaddr_.sin_port = HTONS(port_num);