1#include<winsock2.h> 2#include<iostream> 3#include<string> 4using namespace std; 5#pragma comment(lib, "WS2_32.lib") 6 7string getIP() 8{ 9 WSADATA WSAData; 10 char hostName[256]; 11 if (!WSAStartup(MAKEWORD(2, 0),&WSAData)) 12 { 13 if(!gethostname(hostName,sizeof(hostName))) 14 { 15 hostent *host=gethostbyname(hostName); 16 if(host!=NULL) 17 { 18 return inet_ntoa(*(struct in_addr*)*host->h_addr_list); 19 } 20 } 21 } 22 return "Get IP failed."; 23} 24 25int main() 26{ 27 cout<<"IP地址为:"<<getIP()<<endl; 28 system("pause"); 29 return 0; 30}
C++获取本机IP地址信息
Wesley13
2021-10-11
1269 0 0
点赞
收藏
评论区
加载中...