socket学习笔记——获取域名与IP(linux)
gethostbyname.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 != )
{
printf("usage: %s <addr>\n",argv[]);
exit();
} host = gethostbyname(argv[]);
if(!host)
{
printf("get host error......\n");
exit();
}
printf("official name:%s\n",host->h_name);
for(i = ;host->h_aliases[i];i++)
printf("access %d; %s\n",i+,host->h_aliases[i]);
printf("address type:%s \n",(host->h_addrtype==AF_INET)?"AF_INET":"AFINET6");
for(i = ;host->h_addr_list[i];i++)
printf("IP addr %d: %s \n",i+,inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
return ;
}
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 != )
{
printf("usage :%s <ip>\n",argv[]);
exit();
} memset(&addr,,sizeof(addr));
addr.sin_addr.s_addr = inet_addr(argv[]);
host = gethostbyaddr((char*)&addr.sin_addr,,AF_INET);
if(!host)
{
printf("get host error\n");
exit();
} printf("official name;%s \n",host->h_name);
for(i = ;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 = ;host->h_addr_list[i];i++)
printf("IP addr %d;%s\n",i+,inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
return ;
}
socket学习笔记——获取域名与IP(linux)的更多相关文章
- Linux 学习笔记之超详细基础linux命令(the end)
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 14
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 13
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 12
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 11
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 10
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 9----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 9
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 8
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 7----------------- ...
- Linux 学习笔记之超详细基础linux命令 Part 7
Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...
随机推荐
- html元素背景样式大小调整
定义元素背景设置 background-size属性cover自适应填充背景,background-size: 100% 100%; background-size: 左右比例 上下比例: 再介绍几 ...
- 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- svn 提交错误 400 Bad Reqest MKACTIVITY 请求于XX失败 Conflict Unable to connect to a repository at URL
思路来源:http://www.cnblogs.com/wangyt223/archive/2012/11/22/2782801.html svn 提交错误 400 Bad Reqest MKACTI ...
- PLSQL_Oracle Trigger触发器的基本概念和用法
2014-06-14 Created By BaoXinjian
- mysql给日期增减
有个需求就是判断过期的供求信息,如果用户刷新了则判断过期日期是否小于现在,如果是则自动推迟7天. IF(expire<NOW(),DATE_ADD(NOW(), INTERVAL 7 DAY), ...
- Note++ 的快捷
Notepad++绝对是windows下进行程序编辑的神器之一,要更快速的使用以媲美VIM,必须灵活掌握它的快捷键,下面对notepad++默认的快捷键做个整理(其中有颜色的为常用招数): Ctrl+ ...
- UCOS-信号量(学习笔记)
当事件控制块类型为OS_Event_Type_SEM类型时就是信号量,包含2个内容:信号量计数器OSEventcnt和等待列表OSEventTbl[]. 一创建信号量:OSSemCreat(int16 ...
- Mac下java编译乱码(适用于maven , ant)
将~/.bash_profile中添加如下即可 export LC_ALL=en
- JAVA toString方法
在JAVA中,所有的对象都有toString方法: 创建类时没有定义toString方法,输出对象时,会输出对象的哈希值: 它只是sun公司开发java的时候为了方便所有类的字符串操作而特意加入的一个 ...