VC获取本机IP地址列表


来源:http://www.2cto.com/kf/201102/83560.html


#include <stdio.h> 
#include <winsock2.h> 
#pragma comment(lib,"ws2_32.lib")

int doit(int, char **) 

 char host_name[255]; 
 //获取本地主机名称 
 if (gethostname(host_name, sizeof(host_name)) == SOCKET_ERROR) { 
  printf("Error %d when getting local host name. ", WSAGetLastError()); 
  return 1; 
 } 
 printf("Host name is: %s ", host_name); 
 
 //从主机名数据库中得到对应的“主机” 
 struct hostent *phe = gethostbyname(host_name); 
 if (phe == 0) { 
  printf("Yow! Bad host lookup."); 
  return 1; 
 } 
 
 //循环得出本地机器所有IP地址 
 for (int i = 0; phe->h_addr_list[i] != 0; ++i) { 
  struct in_addr addr; 
  memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); 
  printf("Address %d : %s " , i, inet_ntoa(addr)); 
 } 
 
 return 0; 
}

int main(int argc, char *argv[]) 

 WSAData wsaData; 
 if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { 
  return 255; 
 } 
 
 int retval = doit(argc, argv); 
 
 WSACleanup(); 
 
 return retval; 
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值