Chinaunix首页 | 论坛 | 博客
  • 博客访问: 599021
  • 博文数量: 204
  • 博客积分: 5172
  • 博客等级: 上校
  • 技术积分: 2092
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-08 21:48
个人简介

一个毫无毅力之人的自勉

文章分类

全部博文(204)

文章存档

2014年(1)

2013年(54)

2012年(50)

2011年(94)

2010年(3)

2009年(3)

分类: LINUX

2011-04-26 11:13:26

int gethostname(char *name, size_t len)

这个函数,调用后,会将主机名保存在name里面。而len是name的大小。

以下是例程,编译后只需要运行就知道自己的主机名字了。知道自己名字后,我再调用了一下gethostbyname()来得到主机的一些其他信息。

#include
#include

int main(int argc, char **argv)
{
 struct hostent *hptr;
 char **pptr;
 char hostname[32];
 char str[32];
 
 if( gethostname(hostname,sizeof(hostname)) )
 {
  printf("gethostname calling error\n");
  return 1;
 }
 printf("localhost name:%s\n",hostname);
 if( (hptr = gethostbyname(hostname)) == NULL)
 {
  printf("gethostbyname calling error\n");
  return 1;
 }
 pptr=hptr->h_addr_list;
 for(;*pptr!=NULL;pptr++)
  printf("  address:%s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
 
 return 0;
}

阅读(3372) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~