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)的更多相关文章

  1. Linux 学习笔记之超详细基础linux命令(the end)

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...

  2. Linux 学习笔记之超详细基础linux命令 Part 14

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...

  3. Linux 学习笔记之超详细基础linux命令 Part 13

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...

  4. Linux 学习笔记之超详细基础linux命令 Part 12

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...

  5. Linux 学习笔记之超详细基础linux命令 Part 11

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...

  6. Linux 学习笔记之超详细基础linux命令 Part 10

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 9----------------- ...

  7. Linux 学习笔记之超详细基础linux命令 Part 9

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...

  8. Linux 学习笔记之超详细基础linux命令 Part 8

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 7----------------- ...

  9. Linux 学习笔记之超详细基础linux命令 Part 7

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...

随机推荐

  1. Tortoise SVN Clean up失败的解决方法

    step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe (找到 Precompiled Binaries for ...

  2. HDU3530 子序列

    题目大意:给出一串长度为n的整数串,求最长的一个连续子序列,满足该序列中最大的元素与最小的元素之差大于等于m, 并且小于等于k.n<=100000 分析:维护两个单调队列,一个递增的,维护最小值 ...

  3. 利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO

    原文地址:http://blog.csdn.net/fangzhibin4712/article/details/7179414 前提条件:表sjzdfl  表sjzdxx (使用数据库MySQL) ...

  4. CFLAGS,CPPFLAGS,CXXFLAGS in Makefile

    CC 与 CXX:      这是 C 与 C++ 编译器命令.默认值一般是 “gcc” 与 “g++”. CPPFLAGS will be given to the C preprocessor   ...

  5. 解决AD域认证问题—“未知的身份验证机制”

    场景: Ad认证登录系统,之前正常.不知服务器调了什么,导致无法登录.提示信息如标题. 解决方案: DirectoryEntry adRoot = new DirectoryEntry("L ...

  6. [linux basic]--线程

    /************************************************************************* > File Name: thread1.c ...

  7. 【MySQL】unique列插入重复值解决方案

    当在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE KEY UPDATE子句跳过INSERT.中断操作或者更新旧 ...

  8. flash bulider 生成app无法安装在xcode模拟器上

    使用flash bulider开发app在ios模拟器上运行,出现以下错误 错误提示是isb与当前设备的osx不符合.当前使用airsdk版本是4.0,xcode5.1.1. 查看了air13sdk的 ...

  9. unity c#

    gameObject //获取当前脚本挂载到的游戏对象 在Unity中就算使用了C#进行编写脚本,要输出时不能使用Console类,应当使用print();或者Debug.log(); transfo ...

  10. 树莓派:使用OpenCV调用自带的摄像头.

    总所周知,树莓派上,调用摄像头的指令有raspistill和raspivid.若要使用opencv对摄像头进行调用,不少人会出现 cvCaptureFromCAM(0)函数无法找到Pi Cam的错误情 ...