socket学习笔记——获取域名与IP(linux)
2015-08-17 11:23
344 查看
gethostbyname.c
gethostbyaddr.c
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <arpa/inet.h> #include <netdb.h> int main(int argc,char* argv[]) { int i; struct hostent* host; if(argc != 2) { printf("usage: %s <addr>\n",argv[0]); exit(1); } host = gethostbyname(argv[1]); if(!host) { printf("get host error......\n"); exit(1); } printf("official name:%s\n",host->h_name); for(i = 0;host->h_aliases[i];i++) printf("access %d; %s\n",i+1,host->h_aliases[i]); printf("address type:%s \n",(host->h_addrtype==AF_INET)?"AF_INET":"AFINET6"); for(i = 0;host->h_addr_list[i];i++) printf("IP addr %d: %s \n",i+1,inet_ntoa(*(struct in_addr*)host->h_addr_list[i])); return 0; }
gethostbyaddr.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <netdb.h> int main(int argc,char* argv[]) { int i; struct hostent* host; struct sockaddr_in addr; if(argc != 2) { printf("usage :%s <ip>\n",argv[0]); exit(1); } memset(&addr,0,sizeof(addr)); addr.sin_addr.s_addr = inet_addr(argv[1]); host = gethostbyaddr((char*)&addr.sin_addr,4,AF_INET); if(!host) { printf("get host error\n"); exit(1); } printf("official name;%s \n",host->h_name); for(i = 0;host->h_aliases[i];i++) printf("aliases %d:%s\n",i,host->h_aliases[i]); printf("address type:%s\n",(host->h_addrtype==AF_INET)?"AF_INET":"AF_INET6"); for(i = 0;host->h_addr_list[i];i++) printf("IP addr %d;%s\n",i+1,inet_ntoa(*(struct in_addr*)host->h_addr_list[i])); return 0; }
相关文章推荐
- linux 启动故障处理 -初级
- linux系统时间问题
- Linux进程间通信——信号集函数
- 大约linux的几个问题,你能回答几个?--回复14-20称号
- Linux进程间通信——使用信号
- MySQL-Linux安装
- 批量扫描IP端口程序 (适用于window&linux)
- linux下c语言编程-helloworld
- Linux移植相关链接
- Linux 修改系统时间
- 怎么样sourceforge开源项目发现,centos安装-同htop安装案例
- windows和Linux下查看端口和占用进程
- 树莓派99——nano文本编辑器常用命令
- Linux系统下PHP-FPM的安装和配置教程
- 哪款Linux发行版是你的最佳选择呢?
- linux install zh_CN(ubuntu)
- linux下动态库so文件的一些认识
- linux系统下mysql配置优化初学习入门
- Linux进程间通信——使用信号量
- Linux中查看CPU信息