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----------------- ...
随机推荐
- 文件Copy和文件夹Copy
文件Copy和文件夹Copy using System.Collections.Generic; using System.Linq; using System.Text; using System. ...
- 109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- override 修饰符
override(C# 参考) 要扩展或修改继承的方法.属性.索引器或事件的抽象实现或虚实现,必须使用 override 修饰符. C# abstract class ShapesClass { ab ...
- 在Java中使用Memcached(转)
memcache的Java客户端调用,在网上还是有些少,很多都是php的.如何要想用Java调用memcache的服务,首先要有客户端的支持,我们先下载一个客户端吧.下载地址:https://gith ...
- 内存修改console
#include <stdio.h> #include <windows.h> #include <winuser.h> int main() { int cur_ ...
- 2.Could not open Selected VM debug port (8700). Make sure you do not have another instance of DDMS or of the eclipse plugin running
在eclipse.ini配置文件最后加上:-Djava.net.preferIPv4Stack=true 其他类似
- python(5)字符串处理 (sub,replace,find,index,upper,strip,split,sub翻页
一,sub和replace的用法 re.sub 函数进行以正则表达式为基础的替换工作 re.sub替换到目标字符串中的a,b或者c,并全部替换 另加上sub翻页操作: re.sub('start=\d ...
- 网页设计中常用的Web安全字体
但多数情况下,考虑各个因素的影响我们还是在尽量充分利用这些默认调用的字体实现CSS的编写,这里整理了19个Web安全字体,让你无需任何顾虑的情况下畅快使用. 1, Arial 微软公司的网页核心字体 ...
- 何时要打开stm32的AFIO时钟
STM32的管脚配置一般有2个:Default和rinmap,如果使用default就不需要打开AFIO,否则使用后者就需要打开3个时钟:GPIO时钟.外设功能时钟和AFIO时钟. 一般在涉及外中断配 ...
- JavaScript笔记之数组 keyword(存储和释放&堆栈 & 按值 引用)
1.数组创建及初始化 var obj=new Array(); var arr=[]; 可以延伸为长度一定的,字面量定义数组 2.堆栈 按值传递 引用类型 数组是引用类型,不是值传递, 栈:系桶自动分 ...