加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
net.cc 3.96 KB
一键复制 编辑 原始数据 按行查看 历史
jin 提交于 2024-01-11 10:09 . coding
#include <iterator>
#include <map>
#include <string>
#if 0
#include <iostream>
#include <cstring>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
int main() {
char hostname[256];
char ip[INET_ADDRSTRLEN];
struct hostent *host;
// 获取主机名
if (gethostname(hostname, sizeof(hostname)) == -1) {
perror("获取主机名失败");
return 1;
}
printf("hostname: %s\n", hostname);
// 根据主机名获取IP地址
if ((host = gethostbyname(hostname)) == NULL) {
perror("获取IP地址失败");
return 1;
}
// 遍历IP地址列表
for (int i = 0; host->h_addr_list[i] != NULL; i++) {
strcpy(ip, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));
std::cout << "IP地址: " << ip << std::endl;
}
return 0;
}
#endif
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int get_ips(std::map<std::string, std::string>& ips)
{
struct ifaddrs *ifaddr, *ifa;
int family;
char host[NI_MAXHOST];
if (getifaddrs(&ifaddr) == -1) {
perror("getifaddrs");
exit(EXIT_FAILURE);
}
int num = 0;
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL)
continue;
if (strcmp(ifa->ifa_name, "lo") == 0) {
continue;
}
family = ifa->ifa_addr->sa_family;
printf("family: %d\n", family);
printf("name: %s\n", ifa->ifa_name);
if (family == AF_INET) {
int s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
printf("getnameinfo n = %d\n", s);
if (s != 0) {
printf("getnameinfo() failed: %s\n", gai_strerror(s));
exit(EXIT_FAILURE);
}
printf("%s\n", host);
ips.insert(std::pair<std::string, std::string>(ifa->ifa_name, host));
num++;
}
}
freeifaddrs(ifaddr);
return num;
}
std::string get_one(int n)
{
std::map<std::string, std::string> ips;
int s = get_ips(ips);
if (n > s) {
return "0.0.0.0";
}
std::map<std::string, std::string>::iterator iter;
for (iter = ips.begin(); iter != ips.end(); iter++) {
// std::cout << "Net name: " << iter->first << ", ip: " << iter->second << std::endl;
}
std::map<std::string, std::string>::iterator it = ips.begin();
it = std::next(it, n);
if (it != ips.end()) {
return (*it).second;
}
return "0.0.0.0";
}
std::string get_one(const std::string name)
{
std::map<std::string, std::string> ips;
int s = get_ips(ips);
std::map<std::string, std::string>::iterator it;
it = ips.find(name);
if (it != ips.end()) {
return it->second;
}
return "0.0.0.0";
}
int main (int argc, char *argv[])
{
std::string ip0 = get_one(0);
std::cout << "IP0: " << ip0 << std::endl;
std::string ip1 = get_one(1);
std::cout << "IP1: " << ip1 << std::endl;
std::string ip2 = get_one(2);
std::cout << "IP2: " << ip2 << std::endl;
std::string ip6 = get_one(6);
std::cout << "IP6: " << ip6 << std::endl;
std::string ip00 = get_one("enp3s0");
std::cout << "enp3s0 IP: " << ip00 << std::endl;
std::string ip01 = get_one("wlo1");
std::cout << "wlo1 IP: " << ip01 << std::endl;
std::string ip02 = get_one("wlo1x");
std::cout << "wlo1x IP: " << ip02 << std::endl;
#if 0
int n = get_ips(ips);
printf("get_ips count: %d\n", n);
std::map<std::string, std::string>::iterator iter;
for (iter = ips.begin(); iter != ips.end(); iter++) {
std::cout << "Net name: " << iter->first << ", ip: " << iter->second << std::endl;
}
std::map<std::string, std::string>::iterator iter1= ips.begin();
// iter1 = iter1 + 1;
iter1 = std::next(iter1, 3);
if (iter1 != ips.end()) {
std::cout << "Net[0] name: " << iter1->first << ", ip: " << (*iter1).second << std::endl;
}
else {
std::cout << "std::map iterator end()" << std::endl;
}
#endif
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化