如何判断QtostAddress在Qt5中是IPv4还是IPv6?

时间:2017-10-20 16:28:39

标签: c++ qt qt5 ipv6 ipv4

我可以像这样创建QHostAddress个对象:

QHostAddress addr_ip4("127.0.0.1");
QHostAddress addr_ip6("::1/128");

并测试他们的属性:

qDebug() << "addr_ip4.isNull() =      " << addr_ip4.isNull();
qDebug() << "addr_ip4.isLoopback() =  " << addr_ip4.isLoopback();
qDebug() << "addr_ip4.isMulticast() = " << addr_ip4.isMulticast();

qDebug() << "addr_ip6.isNull() =      " << addr_ip6.isNull();
qDebug() << "addr_ip6.isLoopback() =  " << addr_ip6.isLoopback();
qDebug() << "addr_ip6.isMulticast() = " << addr_ip6.isMulticast();

但是......我怎么知道哪个地址实际上是 IPv4 哪个 IPv6

1 个答案:

答案 0 :(得分:5)

调用QHostAddress::protocol()将返回QAbstractSocket::NetworkLayerProtocol enum,指定地址是IPv4,IPv6,还是其他地址。