获取本机的主机名和ip地址
2013-07-16 14:16
260 查看
#include <iostream>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
string GetEndpoint() {
char host_name[255];
if(gethostname(host_name, 255) == -1) {
return "";
}
cout <<" hostname:" << host_name <<endl;
struct hostent *phe = gethostbyname(host_name);
if (phe == 0) {
return "";
}
string ip;
for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
ip = inet_ntoa(addr);
cout <"ip:" << ip <<endl;
}
return ip+":11315";
}
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
string GetEndpoint() {
char host_name[255];
if(gethostname(host_name, 255) == -1) {
return "";
}
cout <<" hostname:" << host_name <<endl;
struct hostent *phe = gethostbyname(host_name);
if (phe == 0) {
return "";
}
string ip;
for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
struct in_addr addr;
memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
ip = inet_ntoa(addr);
cout <"ip:" << ip <<endl;
}
return ip+":11315";
}
相关文章推荐
- 在InstallShield中通过主机名获取IP地址
- 使用VMware VSphere WebService SDK进行开发 (五)——根据虚拟机的名称获取对应主机的IP地址
- VC中获取主机名和IP地址的方法
- Java获取网络上主机的IP地址
- gethostbyname() -- 用域名或主机名获取IP地址
- C++ 获取本机的Ip地址及主机名
- gethostbyname() -- 用域名或主机名获取IP地址
- 根据主机名获取PC的IP地址
- 获取本机的IP地址(局域网)与主机名称
- ACE获取本机IP地址和主机名
- 在InstallShield中通过主机名获取IP地址的代码
- 在cmd中获取ip地址和主机名
- python 获取主机mac ip地址
- VC中获取主机名和IP地址的方法
- C#获取主机IP地址
- 通过主机名(域名)获取IP地址,主机别名等信息
- 使用VMware VSphere WebService SDK进行开发 (五)——根据虚拟机的名称获取对应主机的IP地址
- JS获取客户端IP地址、MAC和主机名的方法汇总
- [Win32]根据主机名、域名、IP字符串获取IP地址
- Java 实例 - 获取指定主机的IP地址