QTcpSocket Class
The QTcpSocket class provides a TCP socket. More…
| Header | #include <QTcpSocket> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Inherits | QAbstractSocket |
| Inherited by | QSslSocket |
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
QTcpSocket::QTcpSocket(QObject *parent = nullptr)
Creates a TCP socket with the given parent. The socket is not connected.
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.
QTcpSocket::~QTcpSocket()
Destroys the socket, closing any connection.