文章目录
主机(Host)
QHostInfo
c
// 阻塞获取网址信息
QHostInfo info = QHostInfo::fromName("www.bilibili.com");
qDebug() << info.hostName(); // 主机名即域名
qDebug() << info.addresses(); // Ip地址
// 异步获取,如果网址不存在会报错
QHostInfo::lookupHost("www.baidu.com", &a, [=](const QHostInfo info1) { // a是 QCoreApplication a(argc, argv)
qDebug() << info1.hostName();
qDebug() << info1.addresses();
});
// 获取本地主机名
qDebug() << "本地主机名" << QHostInfo::localHostName();
QHostAddress
枚举 | 描述 | 相当于 |
---|---|---|
QHostAddress::Null | 空地址对象 | QHostAddress() |
QHostAddress::LocalHost | IPv4本地主机地址 | QHostAddress(127.0.0.1) |
QHostAddress::LocalHostIPv6 | IPv6本地主机地址 | QHostAddress("::1") |
QHostAddress::Broadcast | IPv4广播地址 | QHostAddress("255.255.255.255") |
QHostAddress::AnyIPv4 | 任意IPv4地址,与此绑定的套接字将只侦听iPV4接口 | QHostAddress("0.0.0.0") |
QHostAddress::AnyIPv6 | 任意IPv6地址 | QHostAddress("::") |
QHostAddress::Any | 双栈任意地址。 |
QNetworkInterface
c
QList<QNetworkInterface> netlist = QNetworkInterface::allInterfaces();
for (auto& net : netlist) {
if (!net.isValid())
continue;
qDebug() << "名称" << net.name();
qDebug() << "描述" << net.humanReadableName();
qDebug() << "类型" << net.type();
qDebug() << "mac地址" << net.hardwareAddress();
for (auto& ip : net.addressEntries())
qDebug() << ip.ip();
qDebug() << "==========================";
}
低级接口
QAbstractSocket
QTcpServer
创建服务器
c
#include <QCoreApplication>
#include <QHostInfo>
#include <QTcpServer>
#include <QTcpSocket>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QTcpServer* server = new QTcpServer;
server->listen(QHostAddress::LocalHost, 6666);
QObject::connect(server, &QTcpServer::newConnection, [=] {
while (server->hasPendingConnections()) { // 如果有连接,读取套接字
QTcpSocket* socket = server->nextPendingConnection();
socket->write("hello world!");
qDebug() << "有新的连接来了";
}
});
return a.exec();
}
QTcpSocket
// 创建客户端
c
QTcpSocket* tcp = new QTcpSocket;
QObject::connect(tcp, &QTcpSocket::errorOccurred, [=](QAbstractSocket::SocketError err) {
qDebug() << err << '\t' << tcp->errorString();
}); // 连接发生错误,报错误原因
QObject::connect(tcp, &QTcpSocket::stateChanged, [=](auto state) {
qDebug() << state;
});
QObject::connect(tcp, &QTcpSocket::connected, [] { qDebug("连接成功"); });
QObject::connect(tcp, &QTcpSocket::readyRead, [=] {
if (tcp->bytesAvailable() > 0)
qDebug() << tcp->readAll();
}); // 如果有可读取数据,读取所有并输出
tcp->connectToHost(QHostAddress::LocalHost, 6666); // 连接本地地址,端口0-1023是被占用的,程序员可用1024-65535
QUdpSocket
单播
c
#include <QCoreApplication>
#include <QNetworkDatagram>
#include <QUdpSocket>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QUdpSocket* udp1 = new QUdpSocket;
QUdpSocket* udp2 = new QUdpSocket;
udp2->bind(QHostAddress::LocalHost, 6666); // 绑定端口号
QObject::connect(udp2, &QUdpSocket::readyRead, [=] {
while (udp2->hasPendingDatagrams()) {
qDebug() << udp2->receiveDatagram().data();
}
}); // 如果udp2里有接收到数据报,就打印
udp1->writeDatagram("hello,world", QHostAddress::LocalHost, 6666); // 发送数据
return a.exec();
}
多播
多播是一对多,与单播步骤是一样的,只有IP地址有区别
多播的地址是D类地址,即224.0.0.0到239.255.255.255之间的IP地址,并被划分为局部连接多播地址 、预留多播地址 和管理权限多播地址3类
- 局部多播地址:224.0.0.0~224.0.0.255之间,这是为路由协议和其他用途保留的地址,路由器并不转发此范围的IP包
- 预留多播地址:224.0.1.0~238.255.255.255之间,可用于全球范围(如Internet)或网络协议。
- 管理权限多播地址:239.0.0.0~239.255.255.255之间,可供组织内部使用,类似于私有IP地址,不能用于Internet,可限制多播范围。
c
#include <QCoreApplication>
#include <QNetworkDatagram>
#include <QUdpSocket>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QUdpSocket* udp1 = new QUdpSocket;
QUdpSocket* udp2 = new QUdpSocket;
// 组播
udp2->bind(QHostAddress::AnyIPv4, 6666, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
udp2->joinMulticastGroup(QHostAddress("224.1.1.1"));
QObject::connect(udp2, &QUdpSocket::readyRead, [=] {
while (udp2->hasPendingDatagrams()) {
qDebug() << udp2->receiveDatagram().data();
}
}); // 如果udp2里有接收到数据报,就打印
udp1->writeDatagram("hello,world", QHostAddress("224.1.1.1"), 6666); // 发送数据
return a.exec();
}
广播
广播就是把多播发送地址改成255.255.255.255,并且客户端 不需要加入多播组,其他操作一样
c
#include <QCoreApplication>
#include <QNetworkDatagram>
#include <QUdpSocket>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
// 广播
QUdpSocket* udp1 = new QUdpSocket;
QUdpSocket* udp2 = new QUdpSocket;
udp2->bind(QHostAddress::AnyIPv4, 6666, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
QObject::connect(udp2, &QUdpSocket::readyRead, [=] {
while (udp2->hasPendingDatagrams()) {
qDebug() << udp2->receiveDatagram().data();
}
}); // 如果udp2里有接收到数据报,就打印
udp1->writeDatagram("hello,world", QHostAddress("255.255.255.255"), 6666); // 发送数据
return a.exec();
}
网络访问
QNetworkAccessManager
QNetworkRequest
QNetworkReply
实操
类似于python爬虫的request请求
c
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
QNetworkAccessManager* manager = new QNetworkAccessManager;
QNetworkRequest request;
request.setUrl(QUrl("https://baike.baidu.com/item/12/1016?fr=ge_ala"));
manager->get(request);
QObject::connect(manager, &QNetworkAccessManager::finished, [=](QNetworkReply* reply) {
qDebug() << QString::fromUtf8(reply->readAll());
});
return a.exec();
}