Convert Url to IP address in C++.
//Convert Url to IP address in C++.
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
//#include<<errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
using namespace std;
int hostname_to_ip(char * hostname , char* ip); // url to ip
int main(){
char hn[] = "www.google.com";
char *hostname = hn;
char ip[100];
hostname_to_ip(hostname, ip);
printf("%s resolved to %s" , hostname , ip);
return 0;
}
int hostname_to_ip(char * hostname , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
return 1;
}
//Convert Url to IP address in C++.
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
//#include<<errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
using namespace std;
int hostname_to_ip(char * hostname , char* ip); // url to ip
int main(){
char hn[] = "www.google.com";
char *hostname = hn;
char ip[100];
hostname_to_ip(hostname, ip);
printf("%s resolved to %s" , hostname , ip);
return 0;
}
int hostname_to_ip(char * hostname , char* ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ( (he = gethostbyname( hostname ) ) == NULL)
{
// get the host info
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip , inet_ntoa(*addr_list[i]) );
return 0;
}
return 1;
}
No comments:
Post a Comment