UNIX环境编程------Unix编程实例------Gethostbyname和gethostname结合使用打印出主机IP地址
2015-11-26 11:11
801 查看
Gethostbyname和gethostname结合使用打印出主机IP地址:
源代码:
#include<unistd.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<stdio.h>
#include<errno.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
charname [50];
intgethnrvl;
inti=0;
struct hostent* hostent_t;
gethnrvl=gethostname(name,sizeof(name));
if(gethnrvl==-1)
{
perror("gethostnameerror");
_exit(-1);
}
printf("%s \n",name);
//使用gethostbyname获取 主机IP
hostent_t =gethostbyname(name);
//打印出IP地址
for(;hostent_t->h_addr_list[i];i++)
{
printf("%s/t",inet_ntoa(*(struct in_addr*)(hostent_t->h_addr_list[i])));
}
}
运行结果:
源代码:
#include<unistd.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<stdio.h>
#include<errno.h>
#include<netdb.h>
#include<stdlib.h>
int main()
{
charname [50];
intgethnrvl;
inti=0;
struct hostent* hostent_t;
gethnrvl=gethostname(name,sizeof(name));
if(gethnrvl==-1)
{
perror("gethostnameerror");
_exit(-1);
}
printf("%s \n",name);
//使用gethostbyname获取 主机IP
hostent_t =gethostbyname(name);
//打印出IP地址
for(;hostent_t->h_addr_list[i];i++)
{
printf("%s/t",inet_ntoa(*(struct in_addr*)(hostent_t->h_addr_list[i])));
}
}
运行结果:
相关文章推荐
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
- 学习算法,编程之路
- 代码库相机图库选择图片
- ip地址数字和字符串格式互转
- 一个用于实现并行执行的 Java actor 库
- cademy的Java习题做后感
- Java内存泄露原因详解
- SSH:Struts2框架(Ognl表达式的讲解和使用说明)
- spring mvc 框架搭建及详解
- PHP OOP面向对象详细介绍总结(代码实例子)
- neo4j的php客户端neoclient连接和使用
- 把vim打造成C++ IDE
- 用VASP计算GW+BSE
- c++11细粒度的线程安全
- Liunx下 ftp服务器的搭建
- Java jdk方式开发webservice并利用Servlet部署到服务器
- java包装器
- Java static关键字
- maven 下载 源码和javadoc命令
- java包装器详解