This piece of code works OK on windows, but gethostbyaddr returns NULL on Linux.
I have tried so many changes, but without any success.
My /etc/host.conf has the following line
order hosts,bind
I run the full code and pass address 11.234.456.74, On windows gethostbyaddr resolves the address and works fine. However on Linux it does not resolve the ip address and returns NULL.
Please help.
#ifdef WIN32
if (init){
WSADATA wsaData;
// Request Winsock version 2.2
if (WSAStartup (MAKEWORD(1, 1), &wsaData) != 0) {
WSACleanup();
exit (EXIT_FAILURE);
}
init = 0;
}
#endif
// Open required socket
p_socket[IP_SOCKET_SOCKET] = socket(AF_INET, server_socket_type, 0);
if ( p_socket[IP_SOCKET_SOCKET] < 0 ) {
#ifdef WIN32
WSACleanup();
#endif
exit (EXIT_FAILURE);
}
destAdrLen = mxGetM(prhs[0]) * mxGetN(prhs[0]) + 1;
destAdr = (char *) mxMalloc(destAdrLen);
if (destAdr == NULL) {
mexErrMsgTxt("mxMalloc(destAdrLen) failed");
}
mxGetString(prhs[0], destAdr, destAdrLen);
destPort = (int) mxGetScalar(prhs[1]);
if (isalpha(destAdr[0])) {
// socket address is a name
hp = gethostbyname(destAdr);
}
else {
// socket address is a number
addr = inet_addr(destAdr);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}