[Linux网络编程]域名转换函数族--使用gethostbyname函数1
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<netdb.h>#include<sys/socket.h>#include<string.h>#include<arpa/inet.h>intmain(intargc,char*argv[]){structhostent*hptr;structin_addrhpaddr;//定义一个地址结构体if((hptr=gethostbyname(argv[1]))==NULL){printf("请输入域名.\n");return1;}else{memcpy(&hpaddr,&hptr->h_addr,4);//拷贝ip地址printf("IP地址为%s.\n",inet_ntoa(hpaddr));}return0;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。