Chinaunix首页 | 论坛 | 博客
  • 博客访问: 759911
  • 博文数量: 111
  • 博客积分: 3895
  • 博客等级: 中校
  • 技术积分: 1300
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-29 21:26
文章分类

全部博文(111)

文章存档

2014年(22)

2013年(8)

2010年(14)

2009年(21)

2008年(46)

我的朋友

分类: LINUX

2008-05-14 08:34:41

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;
}

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