Newbie: Getting IP
void main()
{
char *dns;
char *ip;
char *call;
u_long addr;
ip=new char[256];
dns=new char[256];
if(!InitWinsock())
{
delete dns;
delete ip;
return;
}
scanf("%s",dns);
host=gethostbyname(dns);
call=host->h_addr_list[0];
strcpy(ip,call);
printf("%s",ip);
getch();
//End
delete dns;
delete ip;
WSACleanup();
}
That *should* return the ip address of whatever you type in, right? But, here''s what I get:
google.com
¨n¡É3d¨n¡É#dgoogle.com
And Unless Microsoft has changed any oftheir happy little games, your stuck with it.Patrick Nortin ~TechTV''s "The ScreenSavers"~
Try this:
The
codeka.com - Just click it.
host = gethostbyname( dns );
call = inet_ntoa( *(unsigned long *)host->h_addr_list[0] );
printf( "%s\n", call );
The
h_addr_list
member doesn''t actually point to a string representation of the IP address. If I had my way, I''d have all of you shot!
codeka.com - Just click it.
Here is an example. Hope it helps!
#ifndef WIN32_LEAN_AND_MEAN#define WIN32_LEAN_AND_MEAN#endif#include <iostream>#include <windows.h>#include <winsock.h>using namespace std;void main(){ char input[260]; // Input String WSADATA wsaData; // WSAData LPHOSTENT lpInfo; // Host Information // Init WinSock 2.2 WSAStartup(MAKEWORD(2,2), &wsaData); // Get Input cin.getline(input, 21); // Get the host by actual name not by ip lpInfo = gethostbyname(input); // Display the IP as a string cout << inet_ntoa(*(struct in_addr *) lpInfo->h_addr_list[0]) << endl; // Close WinSock WSACleanup(); return;}//main
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement