gethostname() in windows

gethostname() in windows

本文关键字:windows in gethostname      更新时间:2023-10-16

我发现gethostname()函数返回本地计算机的标准主机名,但我对术语"主机名"有点困惑,它是调用该函数的计算机的名称还是计算机在网络中连接的服务器的名称…

          char szPath[128] = "";
          gethostname(szPath, sizeof(szPath));

如果我想找出本地主机的ip地址,我应该怎么做…

gethostname:返回本地计算机名。

获取IP地址,使用:

   struct hostent *ent = gethostbyname(hostname);
   struct in_addr ip_addr = *(struct in_addr *)(ent->h_addr);
   printf("Hostname: %s, was resolved to: %sn",
           hostname, inet_ntoa(ip_addr));     

来自文档:

gethostname函数检索本地计算机的标准主机名