您的位置:首页 > 理论基础 > 计算机网络

[Linux网络编程]域名转换函数族--使用gethostbyname函数1

2017-11-18 19:45 429 查看
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
int main(int argc,char *argv[])
{
struct hostent *hptr;
struct in_addr hpaddr;   //定义一个地址结构体
if((hptr = gethostbyname(argv[1])) == NULL)
{
printf("请输入域名.\n");
return 1;
}
else
{
memcpy(&hpaddr,&hptr->h_addr,4);  //拷贝ip地址
printf("IP地址为%s.\n",inet_ntoa(hpaddr));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux网络编程