我是靠谱客的博主 大胆可乐,这篇文章主要介绍gethostname 得到主机名,现在分享给大家,希望可以做个参考。

int gethostname(char *name, size_t len)

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

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

#include <netdb.h>
#include <sys/socket.h>

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


from http://blog.csdn.net/szwpc/article/details/518360

最后

以上就是大胆可乐最近收集整理的关于gethostname 得到主机名的全部内容,更多相关gethostname内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(125)

评论列表共有 0 条评论

立即
投稿
返回
顶部