开发环境:
版本:银河麒麟桌面操作系统V10(SP1)
内核:Linux 5.4.18-28.23-bj-generic
CPU:Loongson-3A5000
终端输入 uname -m 查看
架构: loongarch64
下面获取IP地址的gethostname方法在windows 上是可以获取的
在银河麒麟(linux) gethostname 获取不到IP地址,只能获取到回环地址 127.0.1.1
std::vector WHUDP::getIpList()
{
std::vector result;
char name[256]={'\0'};
int getNameRet=::gethostname(name,sizeof(name));
hostent *host=::gethostbyname(name);
if(NULL == host){
return result;
}
in_addr *pAddr=(in_addr*)*host->h_addr_list;
//返回指定主机名的包含主机名字和地址信息的hostent结构的指针
/*struct hostent
{
char *h_name; //正式主机名
char ** h_aliases; //主机别名
short h_addrtype; //主机IP地址类型:IPV4-AF_INET
short h_length; //主机I票地址字节长度,IPV4是4字节,即32位
char ** h_addr_list; //主机的IP地址列表
};*/
for(int i=0; pAddr&&host->h_addr_list[i]!=NULL;i++){
string addr=inet_ntoa(pAddr[i]);
cout<
查找使用下面的可以获取:
#include
#include
#include
#include
#include
#include
#include
#include
std::vector WHUDP::getIpList() {
std::vector result;
int s;
struct ifconf conf;
struct ifreq *ifr;
char buff[BUFSIZ];
int num;
int i;
s = socket(PF_INET, SOCK_DGRAM, 0);
conf.ifc_len = BUFSIZ;
conf.ifc_buf = buff;
ioctl(s, SIOCGIFCONF, &conf);
num = conf.ifc_len / sizeof(struct ifreq);
ifr = conf.ifc_req;
for(i=0;i < num;i++)
{
struct sockaddr_in *sin = (struct sockaddr_in *)(&ifr->ifr_addr);
ioctl(s, SIOCGIFFLAGS, ifr);
if(((ifr->ifr_flags & IFF_LOOPBACK) == 0) && (ifr->ifr_flags & IFF_UP))
{
printf("%s (%s)/n",
ifr->ifr_name,
inet_ntoa(sin->sin_addr));
string addr=inet_ntoa(sin->sin_addr);
result.push_back(addr);
}
ifr++;
}
return result;
}
另一种写法:
static int getIfaceName(char *iface_name, int len)
{
int r = -1;
int flgs, ref, use, metric, mtu, win, ir;
unsigned long int d, g, m;
char devname[20];
FILE *fp = NULL;
if((fp = fopen("/proc/net/route", "r")) == NULL) {
perror("fopen error!\n");
return -1;
}
if (fscanf(fp, "%*[^\n]\n") < 0) {
fclose(fp);
return -1;
}
while (1) {
r = fscanf(fp, "%19s%lx%lx%X%d%d%d%lx%d%d%d\n",
devname, &d, &g, &flgs, &ref, &use,
&metric, &m, &mtu, &win, &ir);
if (r != 11) {
if ((r < 0) && feof(fp)) {
break;
}
continue;
}
strncpy(iface_name, devname, len);
fclose(fp);
return 0;
}
fclose(fp);
return -1;
}
static int getIpAddress(char *iface_name, char *ip_addr, int len)
{
int sockfd = -1;
struct ifreq ifr;
struct sockaddr_in *addr = NULL;
memset(&ifr, 0, sizeof(struct ifreq));
strcpy(ifr.ifr_name, iface_name);
addr = (struct sockaddr_in *)&ifr.ifr_addr;
addr->sin_family = AF_INET;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("create socket error!\n");
return -1;
}
if (ioctl(sockfd, SIOCGIFADDR, &ifr) == 0) {
strncpy(ip_addr, inet_ntoa(addr->sin_addr), len);
close(sockfd);
return 0;
}
close(sockfd);
return -1;
}
//调用
void getIP() {
struct in_addr intaddr;
char iface_name[20];
if(getIfaceName(iface_name, sizeof(iface_name)) < 0) {
printf("get interface name error!\n");
}
if(getIpAddress(iface_name, (char *) &intaddr, 15) < 0) {
printf("get interface ip address error!\n");
}
printf("address:%s\n",(char *) &intaddr);
}
参考:
https://blog.csdn.net/dawnstar_hoo/article/details/4042840
http://blog.chinaunix.net/uid-25885064-id-3457067.html?/14600.html