gethostbyname on local host returns 127.0.0.1 for address

Robert Reif reif at earthlink.net
Wed Aug 18 20:12:49 CDT 2004


On windows gethostbyname for the local host returns
the addresses of the available network interfaces. 
On wine and unix, it only returns the localhost 127.0.0.1. 

Any ideas on how to get the windows behavior?

-------------- next part --------------
#include <stdio.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#endif

int main(int argc, char* argv[])
{
	char			host_name[1024];
	char			** alias;
	struct hostent	* host;
	unsigned int	i = 0;
#ifdef _WIN32
	WSADATA			wsaData;
	WORD			wVersionRequested = MAKEWORD(2, 2); 
	int 			error = WSAStartup(wVersionRequested, &wsaData); 
	
	if (error != 0) 
	{
		fprintf(stderr, "WSAStartup failed\n");
		return 1;
	}

	// Confirm that the Windows Sockets DLL supports 2.2.
	// Note that if the DLL supports versions greater 
	// than 2.2 in addition to 2.2, it will still return 
	// 2.2 in wVersion since that is the version we  
	// requested. 
	if (LOBYTE(wsaData.wVersion) != 2 || 
		HIBYTE(wsaData.wVersion) != 2) 
	{ 
		fprintf(stderr, "couldn't find dll\n");
		return 1;
	}
#endif	

	if (gethostname(host_name, sizeof(host_name)) != 0)
	{
		fprintf(stderr, "gethostname failed\n");
		return 1;
	}

	printf("host: %s\n", host_name);

	host = gethostbyname(host_name);

	if (host == 0)
	{
		fprintf(stderr, "gethostbyname failed\n");
		return 1;
	}

	printf("name of host: %s\n", host->h_name);

	for (alias = host->h_aliases; *alias != NULL; alias++)
		printf("alias: %s\n", *alias);

	printf("length of address: %d\n", host->h_length);

	while (host->h_addr_list[i] != 0)
	{
		struct in_addr	n_addr;

		memcpy(&n_addr, host->h_addr_list[i], sizeof(struct in_addr));

		printf("%d: %s\n", i, inet_ntoa(n_addr));
		i++;
	}

	return 0;
}



More information about the wine-devel mailing list