Click to See Complete Forum and Search --> : function needed to convert hostname to ipaddress


sankris
August 5th, 2005, 02:33 AM
Hi ,
I am writing a program for ping that should support both IPv4 and IPv6 .. i wrote the prog as such the dot ( . ) and colon ( :) is searched and variation is found out ...

My question is " if a host name is given , how to convert it in to a ip address " .. Is there any function to convert it .. i am working on windows APIs and VC++ ...

Thanks ,
Chaitanya

matze42
August 5th, 2005, 02:42 AM
Hi sankris,

try "getaddrinfo" ( WinSock 2 ).
For details see the MSDN or ask again.

Hope this helps,
Matze

NoHero
August 5th, 2005, 02:46 AM
gethostbyname()

matze42
August 5th, 2005, 02:52 AM
gethostbyname()

Sure, but Microsoft says in MSDN:

Note The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

So if you want to stay compatible use "getaddrinfo()".

Greetings,
Matze

golanshahar
August 5th, 2005, 03:47 AM
try this code:

WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );

char Hostname[100];
HOSTENT *pHostEnt;
int **ppaddr;
SOCKADDR_IN sockAddr;
CString addr;

pHostEnt = gethostbyname( "www.codeguru.com");
ppaddr = (int**)pHostEnt->h_addr_list;
sockAddr.sin_addr.s_addr = **ppaddr;
addr = inet_ntoa(sockAddr.sin_addr); //this is your ip address




Cheers

NoHero
August 5th, 2005, 08:36 AM
Sure, but Microsoft says in MSDN:

Note The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

So if you want to stay compatible use "getaddrinfo()".

Greetings,
Matze

This deprecation is irrelevant because gethostbyname() has to work of course of BSD socket compatibility. Use what you want.