Solved

Internet Addresses in MFC

Posted on 1998-11-14
3
184 Views
Last Modified: 2013-11-20
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
0
Comment
Question by:MattLesko
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
Comment Utility
Use the Winsock function 'gethostbyname()'. It returns a 'struct HOSTENT' which has a member called 'h_addr' that holds the IP in binary form (network byte order!). If you need an example how to use it, feel free to ask...
0
 

Author Comment

by:MattLesko
Comment Utility
Sorry, but being new at this, I'd like to request a sample.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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);
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now