gethostbyaddr dns program

Any help on this appreciated.

This is a winsock program designed to prompt the user to enter a domain name.
The program is then supposed to return to the user a resolved ip address of that domain name. The user should also be able to enter an IP address and receive back a domain name.

While the program prompts the user to enter the domain name, all that is ever returned is error#:0

Can anyone shed light on why??
See code


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Declare and initialize variables
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
#include <iostream>

int main(int argc, char **argv)
{
	hostent* remoteHost;
		char* host_name;	
		unsigned int addr;

		//Initialize WINSOCK 2
		WSADATA wsaData; //structure that holds information about current wsock version.
		if(WSAStartup(MAKEWORD(2,2), &wsaData) != 0) //start wsock version 2.2. Function returns
		//0 if successful, otherwise, there was an error that needs to be reported.
		{
		std::cout << "WSAStartup failed: " << WSAGetLastError(); //report the error if there is one
		return 1; //exit the program. 
		}
//-------------------------------------------------------------------------------------------------		
		// User inputs name of host
		printf("Input name of host: ");
//-------------------------------------------------------------------------------------------------
		// Allocate 64 byte char string for host name
		host_name = (char*) malloc(sizeof(char)*64);
		fgets(host_name, 64, stdin);
//-------------------------------------------------------------------------------------------------
		// If the user input is an alpha name for the host, use gethostbyname()
		// If not, get host by addr (assume IPv4)
		if (isalpha(host_name[0])) {   /* host address is a name */
		// if hostname terminated with newline '\n', remove and zero-terminate 
			if (host_name[strlen(host_name)-1] == '\n') 
			host_name[strlen(host_name)-1] = '\0';
			remoteHost = gethostbyname(host_name);
			}
			else  { 
			addr = inet_addr(host_name);
			remoteHost = gethostbyaddr((char *)&addr, 4, AF_INET);
			}

			if (WSAGetLastError() != 0) {
				if (WSAGetLastError() == 11001)
				printf("Host not found...\nExiting.\n");
			}
			else
			printf("error#:%ld\n", WSAGetLastError());

}
Topic archived. No new replies allowed.