Skip to content


#include <QCoroAbstractSocket>
target_link_libraries(myapp QCoro::Network)
QT += QCoroNetwork

QAbstractSocket is a base class for QTcpSocket and QUdpSocket and has some potentially asynchronous operations. In addition to reading and writing, which are provided by QIODevice baseclass and can be used with coroutines thanks to QCoro's QCoroIODevice it provides asynchronous waiting for connecting to and disconnecting from the server.

Since QAbstractSocket doesn't provide the ability to co_await those operations, QCoro provides a wrapper calss QCoroAbstractSocket. To wrap a QAbstractSocket object into the QCoroAbstractSocket wrapper, use qCoro():

QCoroAbstractSocket qCoro(QAbstractSocket &);
QCoroAbstractSocket qCoro(QAbstractSocket *);

Same as QAbstractSocket is a subclass of QIODevice, QCoroAbstractSocket subclasses QCoroIODevice, so it also provides the awaitable interface for selected QIODevice functions. See QCoroIODevice documentation for details.


Waits for the socket to connect or until it times out. Returns bool indicating whether connection has been established (true) or whether the operation has timed out or another error has occurred (false). Returns immediatelly when the socket is already in connected state.

If the timeout is -1, the operation will never time out.

See documentation for QAbstractSocket::waitForConnected() for details.

QCoro::Task<bool> QCoroAbstractSocket::waitForConnected(int timeout_msecs = 30'000);
QCoro::Task<bool> QCoroAbstractSocket::waitForConnected(std::chrono::milliseconds timeout);


Waits for the socket to disconnect from the server or until the operation times out. Returns immediatelly if the socket is not in connected state.

If the timeout is -1, the operation will never time out.

See documentation for QAbstractSocket::waitForDisconnected() for details.

QCoro::Task<bool> QCoroAbstractSocket::waitForDisconnected(timeout_msecs = 30'000);
QCoro::Task<bool> QCoroAbstractSocket::waitForDisconnected(std::chrono::milliseconds timeout);


QCoroAbstractSocket provides an additional method called connectToHost() which is equivalent to calling QAbstractSocket::connectToHost() followed by QAbstractSocket::waitForConnected(). This operation is co_awaitable as well.

If the timeout is -1, the operation will never time out.

See the documentation for [QAbstractSocket::connectToHost()][qtdoc-qabstractsocket-connectToHost] and QAbstractSocket::waitForConnected() for details.

QCoro::Task<bool> QCoroAbstractSocket::connectToHost(const QHostAddress &address, quint16 port,
                                                     QIODevice::OpenMode openMode = QIODevice::ReadOnly,
                                                     std::chrono::milliseconds timeout = std::chrono::seconds(30));
QCoro::Task<bool> QCoroAbstractSocket::connectToHost(const QString &hostName, quint16 port,
                                                     QIODevice::OpenMode openMode = QIODevice::ReadOnly,
                                                     QAbstractSocket::NetworkLayerProtocol protocol = QAbstractSocket::AnyIPProtocol,
                                                     std::chrono::milliseconds timeout = std::chrono::seconds(30));


#include <QCoroTcpSocket>

QCoro::Task<QByteArray> requestDataFromServer(const QString &hostName) {
    QTcpSocket socket;
    if (!co_await qCoro(socket).connectToHost(hostName)) {
        qWarning() << "Failed to connect to the server";
        co_return QByteArray{};

    socket.write("SEND ME DATA!");

    QByteArray data;
    while (!data.endsWith("\r\n.\r\n")) {
        data += co_await qCoro(socket).readAll();

    co_return data;