MattLesko
asked on
Internet Addresses in MFC
How would I go about taking a string (like "www.microsoft.com") and converting it into the dotted decimal system (like 127.0.0.1). Java has the function .getByName(), but I don't see that anywhere in VC++ 66.0. Any suggestions? TIA, Matt Lesko
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No problem ;-)
Here's your example:
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
wVersionRequested = MAKEWORD( 1, 1);
if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
// error initializing winsck...
}
if (!(pHostent = gethostbyname ( "www.microsoft.com")))
{
// an error occured...
}
else
{
printf ( "IP: %s\n", BinIP2StrIP ( *(( long *) pHostent->h_addr));
}
char *BinIP2StrIP ( long lnIP)
{
static char s_acStrIP [ 16];
PUCHAR puc = ( unsigned char *) &lnIP;
unsigned int aun [ 4];
for ( int i = 0; i < 4; aun [ i++] = *puc++);
sprintf ( s_acStrIP, "%d.%d.%d.%d", aun [ 0], aun [ 1], aun [ 2], aun [ 3]);
return ( s_acStrIP);
}
Here's your example:
WORD wVersionRequested;
WSADATA wsaData;
struct hostent *pHostent = NULL;
wVersionRequested = MAKEWORD( 1, 1);
if ( nErr = WSAStartup ( wVersionRequested, &wsaData))
{
// error initializing winsck...
}
if (!(pHostent = gethostbyname ( "www.microsoft.com")))
{
// an error occured...
}
else
{
printf ( "IP: %s\n", BinIP2StrIP ( *(( long *) pHostent->h_addr));
}
char *BinIP2StrIP ( long lnIP)
{
static char s_acStrIP [ 16];
PUCHAR puc = ( unsigned char *) &lnIP;
unsigned int aun [ 4];
for ( int i = 0; i < 4; aun [ i++] = *puc++);
sprintf ( s_acStrIP, "%d.%d.%d.%d", aun [ 0], aun [ 1], aun [ 2], aun [ 3]);
return ( s_acStrIP);
}
ASKER