Solved

Internet Addresses in MFC

Posted on 1998-11-14
3
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 1324705
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
ID: 1324706
Sorry, but being new at this, I'd like to request a sample.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1324707
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 122
fizzArray2 challenge 1 112
no14 challenge 14 78
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 1,688
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

738 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