#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <iostream>
#include <string.h>
#include <errno.h>
using namespace std;
int main(int argc, char *argv [])
{
char sz [] = "Hello, World!"; //Hover mouse over "sz" while debugging to see its contents
cout << sz << endl; //<================= Put a breakpoint here
struct hostent *host=NULL;
char hostname [] = "www.163.com";
struct sockaddr_in addr_in;
char szText[32] = {0};
extern int h_errno;
if ((host = gethostbyname(hostname)) != NULL)
{
memcpy(&addr_in.sin_addr.s_addr, host->h_addr_list, 4);
printf("Domain name:%s\n", hostname);
printf("IP length: %d\n", host->h_length);
printf("Type:%d\n", host->h_addrtype);
printf("address: %s\n", inet_ntop(host->h_addrtype, host->h_addr, szText, sizeof(szText)));
}
else
{
char * mesg = strerror(errno);
printf("%d\n", errno);
printf("%s\n", mesg);
}
char addr [] = "220.162.97.209";
host=gethostbyaddr(addr,sizeof(addr),AF_INET);
if (host!=NULL)
{
memcpy(&addr_in.sin_addr.s_addr, host->h_addr_list, 4);
printf("Domain name:%s\n", hostname);
printf("IP length: %d\n", host->h_length);
printf("Type:%d\n", host->h_addrtype);
printf("address: %s\n", inet_ntop(host->h_addrtype, host->h_addr, szText, sizeof(szText)));
}
else
{
char * mesg = strerror(errno);
printf("%d\n", errno);
printf("%s\n", mesg);
}
return 0;
}
/*
Hello, World!
Domain name:www.163.com
IP length: 4
Type:2
address: 220.162.97.209
Domain name:www.163.com
IP length: 15
Type:2
address: 50.50.48.46
*/
gethostbyname
最新推荐文章于 2024-12-05 11:43:25 发布