标题:C++获取本机地址信息
只看楼主
lanqiaojun
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2008-9-29
结帖率:100%
 问题点数:0 回复次数:0 
C++获取本机地址信息
int main()
{
   WORD wVersionRequested = MAKEWORD(1, 1);
   WSADATA wsaData;
   if (WSAStartup(wVersionRequested, &wsaData)) {
      printf("WSAStartup failed %s\n", WSAGetLastError());
      return -1;
   }
   char hostname[128];
   int res = gethostname(hostname, sizeof(hostname));
   if (res != 0) {
      printf("Error: %u\n", WSAGetLastError());
      return -1;
   }
   printf("hostname=%s\n", hostname);
   hostent* pHostent = gethostbyname(hostname);
   if (pHostent==NULL)
  {
      printf("Error: %u\n", WSAGetLastError());
      return -1;
   }

   hostent& host = *pHostent;
   sockaddr_in sain;
   for (int ilen=0;host.h_addr_list[ilen];ilen++)
  {
      memcpy ( &sain.sin_addr.s_Sun, host.h_addr_list[ilen],host.h_length);
      printf("Address: %s\n", inet_ntoa(sain.sin_addr));   
   }
   WSACleanup();
   return 0;
}     


空间不能用`借个位置记录下
搜索更多相关主题的帖子: 地址 获取 
2008-12-07 15:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-248149-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 1.878578 second(s), 9 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved