Tutorial :gethostbyaddr() has error 11001



Question:

I'm writing simple program to communicate between smart devices and I receive 11001 when calling gethostbyaddr(). Both devices show they are connected to the same network, but from msdn document 11001 error is Host not found. No such host is known. Does anybody have any suggestion, thanks? My code is below.

void InitializeSocket()  {      WORD socketVersion;      WSADATA wsaData;      SOCKADDR_IN serverInfo;      int returnVal;      LPHOSTENT remotHost;        socketVersion = MAKEWORD(2,2);      WSAStartup(socketVersion, &wsaData);        in_addr iaHost;        //iaHost.s_addr = inet_addr("120.15.22.14");      iaHost.S_un.S_un_b.s_b1 = 120;      iaHost.S_un.S_un_b.s_b2 = 15;      iaHost.S_un.S_un_b.s_b3 = 22;      iaHost.S_un.S_un_b.s_b4 = 14;      remotHost = gethostbyaddr((const char *)&iaHost, sizeof(struct in_addr),          AF_INET);        if(iaHost.s_addr == INADDR_NONE)      {          MessageBox(NULL, TEXT("inet_addr has invalid address"),              TEXT("inet_addr"), MB_OK);          WSACleanup();          closesocket(theSocket);          return;      }  

 

    if(!remotHost)      {          returnVal = WSAGetLastError();          WSACleanup();          closesocket(theSocket);          return;      }        theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);          if(theSocket == INVALID_SOCKET)      {          returnVal = WSAGetLastError();          WSACleanup();          closesocket(theSocket);          return;      }        serverInfo.sin_family = AF_INET;      serverInfo.sin_addr = *((LPIN_ADDR)*remotHost->h_addr_list);      serverInfo.sin_port = htons(8888);        //Connect to the server      returnVal = connect(theSocket, (LPSOCKADDR)&serverInfo,          sizeof(struct sockaddr));        if(returnVal == SOCKET_ERROR)      {          returnVal = WSAGetLastError();          WSACleanup();          closesocket(theSocket);          return;      }  }  


Solution:1

Error 11001 is a generic DNS lookup error so I have only one question. Does the 120.15.22.14 address actually exist in DNS?

You can find this out by typing

nslookup 120.15.22.14  

from the command line.

If you get an error, it's because DNS doesn't know anything about those addresses, so a gethostbyaddr() will not be able to give you any information.

Update:

Answering your points:

I just type nslookup 120.15.22.14, but it returns back as cant find 120.15.22.14. Non-existent domain.

Since nslookup 120.15.22.14 returns an error, there is no entry in DNS for that IP address. That's your problem.

I look at my device settings for the DNS and it sets to 120.20.32.10 which is different 120.15.22.14 so does it mean I haven't configured the DNS?

If 120.20.32.10 is what's in DNS for your device, and 120.15.22.14 is the actual address, then DNS is wrong. Plain and simple. Fix DNS.

will it produce the same result if I use gethostbyname() instead of gethostbyaddr()?

If you want to turn a DNS name into an IP address, use gethostbyname(). To turn an IP address into a DNS name, use gethostbyaddr().

For connecting to a remote host, you would normally use the gethostbyname() call since the IP address of the host could change at any time. Provided DNS always accurately represents the IP address of that host, that's the preferred way.


Solution:2

I just type nslookup 120.15.22.14, but it returns back as cant find 120.15.22.14. Non-existent domain.

I look at my device settings for the DNS and it sets to 120.20.32.10 which is different 120.15.22.14 so does it mean I haven't configured the DNS?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »