#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
#pragma comment(lib, "WS2_32.lib")
string getIP()
{
WSADATA WSAData;
char hostName[256];
if (!WSAStartup(MAKEWORD(2, 0),&WSAData))
{
if(!gethostname(hostName,sizeof(hostName)))
{
hostent *host=gethostbyname(hostName);
if(host!=NULL)
{
return inet_ntoa(*(struct in_addr*)*host->h_addr_list);
}
}
}
return "Get IP failed.";
}
int main()
{
cout<<"IP地址为:"<<getIP()<<endl;
system("pause");
return 0;
}
C/C++ 获取本机IP地址信息
原创
©著作权归作者所有:来自51CTO博客作者LyShark的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:Linux系统用户与属组管理
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
获取IP地址
端点(API接口)访问获取IP地址!
IP Java SpringBoot\ -
C++获取本机的ip地址程序
上面是头文件 源代码文件
#include 主机名 ios 其他 -
C#获取本机IP方法,获取本机局域网IP地址方法
1. 2.使用IPHostEntry获取本机局域网地址 3.通过向网站向一些提供IP查询的网站发送webrequest,然后分析返回的数据流 4.通过获取CMD里ipconfig命令的结果来得到IP
获取本机IP地址 获取本机局域网IP地址 ipad .net 内网