• C++获取本机IP地址(Win+linux)


    获取本机IP地址(Win+linux)

    #include <iostream>
    #include <optional>
    #include <string>
    
    #ifdef _WIN32
    #include <winsock2.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    #include <stdlib.h>
    #pragma comment(lib, "IPHLPAPI.lib")
    #else
    #include <sys/types.h>
    #include <ifaddrs.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #endif
    
    using namespace std;
    
    #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
    #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
    
    std::optional<std::string> GetIpAddress()
    {
    #ifdef _WIN32
    	PIP_ADAPTER_INFO pAdapterInfo;
    	PIP_ADAPTER_INFO pAdapter = NULL;
    	DWORD dwRetVal = 0;
    
    	ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    	pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(sizeof(IP_ADAPTER_INFO));
    	if (pAdapterInfo == NULL) {
    		return std::nullopt;
    	}
    	
    	if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
    		FREE(pAdapterInfo);
    		pAdapterInfo = (IP_ADAPTER_INFO *)MALLOC(ulOutBufLen);
    		if (pAdapterInfo == NULL) {
    			return std::nullopt;
    		}
    	}
    	
    	std::string ip_address;
    	if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
    		pAdapter = pAdapterInfo;
    		if (pAdapter) {
    			ip_address = pAdapter->IpAddressList.IpAddress.String;
    		}
    	}
    	
    	if (pAdapterInfo) {
    		FREE(pAdapterInfo);
    	}
    	
    	if (!ip_address.empty()) {
    		return ip_address;
    	}
    	return std::nullopt;
    
    #else
    	struct ifaddrs * ifAddrStruct = NULL;
    	struct ifaddrs * ifa = NULL;
    	void * tmpAddrPtr = NULL;
    
    	getifaddrs(&ifAddrStruct);
    	std::string ip_address;
    	for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
    		if (!ifa->ifa_addr) {
    			continue;
    		}
    		if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
    		  // is a valid IP4 Address
    			tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
    			char addressBuffer[INET_ADDRSTRLEN];
    			inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
    	
    			if (strcmp(ifa->ifa_name, "lo")) {
    				continue;
    			}
    			else {
    				ip_address = addressBuffer;
    				if (!ip_address.empty()) {
    					break;
    				}
    			}
    		}
    	}
    	if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
    	if (!ip_address.empty()) {
    		return ip_address;
    	}
    	else {
    		return std::nullopt;
    	}
    
    #endif
    }
    
    int main()
    {
    	auto res =  GetIpAddress();
    	if (res.has_value())
    	{
    		auto ip = res.value();
    		std::cout << ip << std::endl;
    	}
        std::cout << "Hello World!
    ";
    }
    

    参考链接:https://docs.microsoft.com/en-us/windows/win32/api/iphlpapi/nf-iphlpapi-getadaptersinfo

  • 相关阅读:
    vue nextTick使用
    Vue slot插槽内容分发
    vue 项目设置实现通过本地手机访问
    vue router mode模式在webpack 打包上线问题
    html设置 hight100%问题
    更新模块通知栏显看不到当前进度,以及更新下载中可以清理通知问题,华为强制更新退出软件后台下载不显示通知问题
    ScrollView下嵌套GridView或ListView默认不在顶部的解决方法
    文件说明注释
    EditText双光标问题
    原 android重启应用(应用重新启动自身)
  • 原文地址:https://www.cnblogs.com/LuckCoder/p/14310539.html
Copyright © 2020-2023  润新知