6.0.0-rc4Source

QTcpSocket Class

The QTcpSocket class provides a TCP socket. More…

Header#include <QTcpSocket>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
InheritsQAbstractSocket
Inherited byQSslSocket

List of all members, including inherited members

Public Functions

QTcpSocket(QObject *parent = nullptr)
~QTcpSocket()

Protected Functions

QTcpSocket(SocketType type, QObject *parent)

Detailed Description

QTcpSocket is a stream-oriented QAbstractSocket for TCP clients. Connect with connectToHost() and use the QIODevice read and write functions:

QTcpSocket socket;
socket.connectToHost("example.com", 80);
if (socket.waitForConnected(3000)) {
    socket.write("GET / HTTP/1.0\r\n\r\n");
    socket.waitForReadyRead(3000);
    QByteArray reply = socket.readAll();
}

See also QAbstractSocket, QUdpSocket and QTcpServer.

Differences from Qt

QTcpSocket adds nothing to QAbstractSocket and there is no QSslSocket subclass. The protected constructor taking a SocketType replaces Qt's constructor that takes a private class.

Member Function Documentation

[explicit] QTcpSocket::QTcpSocket(QObject *parent = nullptr)

Creates a TCP socket with the given parent. The socket is not connected.

[protected] QTcpSocket::QTcpSocket(SocketType type, QObject *parent)

Creates a socket of the given type with the given parent. This constructor is for subclasses that need a socket type other than TcpSocket.

[override] QTcpSocket::~QTcpSocket()

Destroys the socket, closing any connection.