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----------------- ...
随机推荐
- Python装饰器通用样式
装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象.它经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理. ...
- golang的连接池例子
github.com/jolestar/go-commons-pool 测试代码 package main import ( "github.com/jolestar/go-commons- ...
- worker_pool的例子
鉴于poolboy的坑,pooler不支持r18,又有在知乎上看到大神推荐worker_pool这个进程池框架(工作者进程在创建时崩溃,worker_pool不受影响),所以研究了下,贴个小例子 my ...
- 287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), pro ...
- (WPF, MVVM) Event 处理
WPF的有些UI元素有Command属性可以直接实现绑定,如Button 但是很多Event的触发如何绑定到ViewModel中的Command呢? 答案就是使用EventTrigger可以实现. 继 ...
- 单选按钮选中js的处理
function FinancialinfosetController($scope, $http, $timeout, $location, $rootScope, $routeParams) { ...
- SOA_环境安装系列3_Oracle Weblogic安装和环境搭建(案例)
2014-01-03 Created By BaoXinjian
- PLSQL_性能优化系列19_Oracle Explain Plan解析计划通过Profile绑定
20150529 Created By BaoXinjian
- redis 安装并设置为开机启动服务
安装 1.下载redis,wget http://download.redis.io/releases/redis-3.0.1.tar.gz 解压:tar zxvf redis3.0.1.tar.gz ...
- [物理学与PDEs]第5章 弹性力学
[物理学与PDEs]第5章第1节 引言 [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.1 变形梯度张量 [物理学与PDEs]第5章第2节 变形的描述, 应变张量 2.2 Cauchy ...