C/C++ 获取本机IP地址信息
2023-02-18 16:44:47 时间
#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;
}
相关文章
- Linux 文件的打包压缩
- Linux 常用命令总结
- Linux 下的输入输出和重定向示例
- CentOS Linux 的安装
- Linux 中的文件简单说明
- Linux 中的内部命令和外部命令
- Debian Linux 的安装
- Arch Linux 的安装
- Linux 下搭建 Hive 环境
- Linux 下搭建 HBase 环境
- Linux 下搭建 Hadoop 环境
- Linux 下配置 hosts 并设置免密登录
- Linux 下搭建 Kafka 环境
- Linux 下指定端口开放访问权限
- Linux 下模拟制作块设备并挂载
- Jgit的使用笔记
- 利用Github Action实现Tornadofx/JavaFx打包
- 叹息!GitHub Trending 即将成为历史!
- 微软软了?开源社区讨论炸锅,GitHub CEO 亲自来答
- GitHub Trending 列表频现重复项,前后端都没去重?