主机(Host)

QHostInfo

    // 阻塞获取网址信息
    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

QNetworkInterface

    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() << "==========================";        
    }

Qt第十九章 网络编程-LMLPHP

低级接口

QAbstractSocket

QTcpServer

创建服务器

#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();
}

Qt第十九章 网络编程-LMLPHP

QTcpSocket

// 创建客户端

    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

Qt第十九章 网络编程-LMLPHP

QUdpSocket

单播

#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();
}

多播

#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,并且客户端 不需要加入多播组,其他操作一样

#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请求

#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();
}

Qt第十九章 网络编程-LMLPHP

08-25 10:18