一、异步读写

在 Qt 中实现 TCP 客户端和服务器的同步和异步读写消息涉及使用 QTcpSocketQTcpServer 类。这两个类提供了用于建立 TCP 连接、发送和接收数据的功能。下面是一个简单的示例,演示了如何在 Qt 中实现 TCP 客户端和服务器的同步和异步读写消息:

TCP 服务器端示例(异步)

#include <QCoreApplication>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTcpServer server;
    server.listen(QHostAddress::Any, 1234); // 监听端口 1234

    QObject::connect(&server, &QTcpServer::newConnection, [&](){
        QTcpSocket* clientSocket = server.nextPendingConnection();
        QObject::connect(clientSocket, &QTcpSocket::readyRead, [&](){
            QByteArray requestData = clientSocket->readAll();
            qDebug() << "Received request from client:" << requestData;

            // Echo back the received data
            clientSocket->write(requestData);
        });
    });

    qDebug() << "TCP server started. Listening on port 1234...";

    return a.exec();
}

TCP 客户端端示例(异步)

#include <QCoreApplication>
#include <QTcpSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTcpSocket client;
    client.connectToHost("127.0.0.1", 1234); // 连接到服务器的 IP 地址和端口

    QObject::connect(&client, &QTcpSocket::connected, [&](){
        qDebug() << "Connected to server.";

        // Send a message to the server
        client.write("Hello, Server!");
    });

    QObject::connect(&client, &QTcpSocket::readyRead, [&](){
        QByteArray responseData = client.readAll();
        qDebug() << "Received response from server:" << responseData;
    });

    return a.exec();
}

示例简单演示了如何在 Qt 中实现 TCP 客户端和服务器的异步读写消息。在异步通信中,使用信号和槽机制来处理数据的接收和发送。

二、同步读写

以下是一个简单的示例,演示了如何在 Qt 中实现 TCP 客户端和服务器的同步读写消息:

TCP 服务器端示例(同步)

#include <QCoreApplication>
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTcpServer server;
    server.listen(QHostAddress::Any, 1234); // 监听端口 1234

    if (!server.isListening()) {
        qDebug() << "Failed to start TCP server.";
        return 1;
    }

    QTcpSocket* clientSocket = nullptr;

    while (true) {
        if (server.hasPendingConnections()) {
            clientSocket = server.nextPendingConnection();
            break;
        }
    }

    if (clientSocket) {
        qDebug() << "Client connected.";

        // Read data from client
        QByteArray requestData = clientSocket->readAll();
        qDebug() << "Received request from client:" << requestData;

        // Echo back the received data
        clientSocket->write(requestData);
    }

    return a.exec();
}

TCP 客户端端示例(同步)

#include <QCoreApplication>
#include <QTcpSocket>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QTcpSocket client;
    client.connectToHost("127.0.0.1", 1234); // 连接到服务器的 IP 地址和端口

    if (!client.waitForConnected()) {
        qDebug() << "Failed to connect to server.";
        return 1;
    }

    qDebug() << "Connected to server.";

    // Send a message to the server
    client.write("Hello, Server!");

    if (!client.waitForBytesWritten()) {
        qDebug() << "Failed to write data to server.";
        return 1;
    }

    if (!client.waitForReadyRead()) {
        qDebug() << "Failed to read response from server.";
        return 1;
    }

    // Read response from server
    QByteArray responseData = client.readAll();
    qDebug() << "Received response from server:" << responseData;

    return a.exec();
}

示例简单演示了如何在 Qt 中实现 TCP 客户端和服务器的同步读写消息。在同步通信中,使用 waitForConnectedwaitForBytesWrittenwaitForReadyRead 等方法来等待连接建立、数据写入和数据读取完成。这种方式适用于需要确保数据传输完整性和顺序性的场景。

04-24 08:31