Linux C++获取系统名称和ip

使用封装的这个函数获取系统的名称和ip。

#include  /* cout */
#include /* gethostname */
#include  /* struct hostent */
#include  /* inet_ntop */

bool GetHostInfo(std::string& hostName, std::string& Ip) {
	char name[256];
	gethostname(name, sizeof(name));
	hostName = name;
	
	struct hostent* host = gethostbyname(name);
	char ipStr[32];
	const char* ret = inet_ntop(host->h_addrtype, host->h_addr_list[0], ipStr, sizeof(ipStr));
	if (NULL==ret) {
		std::cout << "hostname transform to ip failed";
		return false;
	}
	Ip = ipStr;
	return true;
}

进行测试:

//以下测试
int main(int argc, char *argv[]) {
	std::string hostName;
	std::string Ip;
	
	bool ret = GetHostInfo(hostName, Ip);
	if (true == ret) {
		std::cout << "hostname: " << hostName << std::endl;
		std::cout << "Ip: " << Ip << std::endl;
	}
	return 0;
}

输出结果:略

你可能感兴趣的:(c++使用)