function needed to convert hostname to ipaddress
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
Re: function needed to convert hostname to ipaddress
Hi sankris,
try "getaddrinfo" ( WinSock 2 ).
For details see the MSDN or ask again.
Hope this helps,
Matze
Re: function needed to convert hostname to ipaddress
Re: function needed to convert hostname to ipaddress
Quote:
Originally Posted by NoHero
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
Re: function needed to convert hostname to ipaddress
try this code:
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
Re: function needed to convert hostname to ipaddress
Quote:
Originally Posted by matze42
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.