直接上代码,大家想要可以自行封装成函数
方法一:使用QHostAddress
//QList<QHostAddress> address = QNetworkInterface::allAddresses();
foreach (QHostAddress ptr , QNetworkInterface::allAddresses())
{
if(ptr.protocol() == QAbstractSocket::IPv4Protocol){// 获取ipv4地址
if(!ptr.isLoopback()) // 过滤本地回环127.0.0.1
qDebug() << "address: " <<ptr.toString()<< endl; // 在这里输出本地局域网ip
}
}
方法二:使用QNetworkInterface从本地所有网卡获取活跃的IP
//qt获得网卡信息状态
QString rip="";
//QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
foreach (QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
if (!netInterface.isValid())//包含有关网络接口的有效信息,则返回true。
continue;
QNetworkInterface::InterfaceFlags flags = netInterface.flags();
// 网络接口处于活动状态 && 不是本地回环地址
if (flags.testFlag(QNetworkInterface::IsRunning)
&& !flags.testFlag(QNetworkInterface::IsLoopBack))
{
// 遍历每一个IP地址
QList<QNetworkAddressEntry> entryList = netInterface.addressEntries();
foreach(QNetworkAddressEntry entry, entryList)
{
if(entry.ip().toString()!="" && entry.ip().toString()!="0.0.0.0")
{
rip=entry.ip().toString();
break;//获取到第一个活跃的跳出
}
}
}
}
QT ip地址,格式为 “::ffff:127.0.0.1” 解决方法
买一送一:C++根据域名获取IP地址
使用gethostbyname()
函数实现。win平台可能要包含<WinSock2.h>
和绑定WSAData
<netdb.h>
定义内的 struct hostent
结构如下:
struct hostent {
char *h_name; /* 官方域名,但是似乎并不绝对,百度就是www.a.shifen.com */
char **h_aliases; /* 别名列表,是个二级指针 */
int h_addrtype; /* 地址类型,为AF_INET或AF_INET6之一,百度返回2,故为真应该是AF_INET */
int h_length; /* 保存IP地址长度。IPv4 的长度为 4 个字节,IPv6 的长度为 16 个字节。 */
char **h_addr_list; /* 地址列表,以NULL结尾 */
}
#define h_addr h_addr_list[0] /* 向后兼容,返回地址列表的第一个地址,注意返回的是指针 */
上代码:
#include <iostream>
#include <string>
extern "C"{
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
}
#ifdef __WIN32__
#include <WinSock2.h>
#else
#include <netdb.h>
#endif
using namespace std;
int main(void)
{
string buf;
// 链接和绑定sock-api库,要在windows下面开发socket都需要这样做
#ifdef __WIN32__
WSAData wsadata;
WSAStartup(MAKEWORD(2, 2), &wsadata);
#endif
// 分析之
struct hostent * conText = gethostbyname("www.baidu.com");
if(!conText){
cout << "get host name ERROR!";
#ifdef __WIN32__
WSACleanup();
#endif
return 0;
}
// 获取主名和别名
cout << "the office name :" << conText->h_name << endl;
for (char ** tmp = &conText->h_aliases[0]; *tmp != NULL;tmp++)
cout << "the alternative name:" << *tmp << endl;
//获取地址类型
cout << " Addres type :" << conText->h_addrtype << endl;
// 获取IP地址
for (char ** tmp = &conText->h_addr_list[0]; *tmp != NULL;tmp++){
// struct in_addr tmpAddr = *reinterpret_cast<struct in_addr*>(*tmp);
cout << "the ip :" << inet_ntoa( *reinterpret_cast<struct in_addr*>(*tmp) ) << endl;
cout << "直接获得" << inet_ntoa( *reinterpret_cast<struct in_addr*>(conText->h_addr) ) << endl;
#ifdef __WIN32__
WSACleanup();
#endif
return 0;
}
echo 输出彩色字符串:
这段是个人常用,但单独写个文章又感觉没必要,于是加的,方便查看_(:з」∠)_
- 模板:
echo -e "\e[xx;xxm 你的内容 \e[0m"
-
解析:
-e
是echo的转义字符意思。
\e[xxm
xx是数字选项,选择字体属性、颜色、背景色用的,对顺序没有要求 -
各类数字选项:
背景色:
黑色背景:40
红色背景:41
绿色背景:42
黄色背景:43
蓝色背景:44
洋红背景:45
青色背景:46
白色背景:47
字体颜色:
黑色字体:30
红色字体:31
绿色字体:32
黄色字体:33
蓝色字体:34
洋红字体:35
青色字体:36
白色字体:37
属性
0: OFF
1: 高亮显示
3: 斜体
4: 下划线
5: 闪烁
7: 反色显示
8: 不可见
9: 删除线
echo -e "\e[9;32m hello \e[0m" #绿色字体、删除线的hello