6.0.0-rc4Source

QTcpServer Class

The QTcpServer class provides a TCP-based server. More…

Header#include <QTcpServer>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
InheritsQObject
Inherited byQSslServer

List of all members, including inherited members

Public Functions

QTcpServer(QObject *parent = nullptr)
~QTcpServer()
boollisten(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
voidclose()
boolisListening() const
voidsetMaxPendingConnections(int numConnections)
intmaxPendingConnections() const
quint16serverPort() const
QHostAddressserverAddress() const
qintptrsocketDescriptor() const
boolsetSocketDescriptor(qintptr socketDescriptor)
boolwaitForNewConnection(int msec = 0, bool *timedOut = nullptr)
virtual boolhasPendingConnections() const
virtual QTcpSocket *nextPendingConnection()
QAbstractSocket::SocketErrorserverError() const
QStringerrorString() const
voidpauseAccepting()
voidresumeAccepting()

Signals

Protected Functions

virtual voidincomingConnection(qintptr handle)
voidaddPendingConnection(QTcpSocket *socket)

Detailed Description

Call listen() to start accepting connections, then connect to newConnection() and fetch each accepted socket with nextPendingConnection():

QTcpServer server;
QObject::connect(&server, &QTcpServer::newConnection, [&] {
    QTcpSocket *client = server.nextPendingConnection();
    client->write("hello\n");
});
server.listen(QHostAddress::Any, 8080);

Accepted sockets are children of the server. The server stops accepting once maxPendingConnections() sockets are waiting and resumes as they are taken.

See also QTcpSocket and QAbstractSocket.

Differences from Qt

There is no listenBacklogSize(), setListenBacklogSize() or proxy support; the backlog is fixed at 128. Sockets are always bound with SO_REUSEADDR. serverError() and errorString() report only the errors of listen(), setSocketDescriptor() and accept. Servers must be used from a single thread.

On Zephyr the connections that a server can hold are bounded by CONFIG_ZVFS_OPEN_MAX, CONFIG_NET_MAX_CONTEXTS and CONFIG_NET_MAX_CONN. See Nt on Zephyr.

Member Function Documentation

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

Constructs a TCP server with the given parent. The server is not listening.

[override] QTcpServer::~QTcpServer()

Destroys the server, closing it if it is listening.

See also close().

[signal] void QTcpServer::acceptError(QAbstractSocket::SocketError socketError)

This signal is emitted when accepting a new connection results in an error. The socketError parameter describes the type of error.

See also serverError().

[protected] void QTcpServer::addPendingConnection(QTcpSocket *socket)

Called by incomingConnection() to add socket to the queue of pending connections. The server becomes the socket's parent.

See also nextPendingConnection() and incomingConnection().

void QTcpServer::close()

Closes the server so it no longer listens for incoming connections. Connections that were already accepted remain valid.

See also listen() and isListening().

QString QTcpServer::errorString() const

Returns a human-readable description of the last error that occurred.

See also serverError().

[virtual] bool QTcpServer::hasPendingConnections() const

Returns true if the server has pending connections; otherwise returns false.

See also nextPendingConnection() and newConnection().

[virtual, protected] void QTcpServer::incomingConnection(qintptr handle)

This virtual function is called when a new connection is available. handle is the native descriptor of the accepted socket. The default implementation creates a QTcpSocket on it and adds it to the pending connections with addPendingConnection(). Reimplement it to create a different socket type.

See also newConnection() and addPendingConnection().

bool QTcpServer::isListening() const

Returns true if the server is listening for incoming connections; otherwise returns false.

See also listen() and close().

bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)

Tells the server to listen for incoming connections on address and port. A port of 0 lets the system choose a free port. Returns true on success; otherwise sets serverError() and errorString() and returns false.

Listening on QHostAddress::Any accepts both IPv4 and IPv6 connections when the system supports it.

See also isListening(), close() and serverPort().

int QTcpServer::maxPendingConnections() const

Returns the maximum number of pending accepted connections.

See also setMaxPendingConnections().

[signal] void QTcpServer::newConnection()

This signal is emitted every time a new connection is available. Call nextPendingConnection() to accept it.

See also hasPendingConnections() and nextPendingConnection().

[virtual] QTcpSocket * QTcpServer::nextPendingConnection()

Returns the next pending connection as a connected QTcpSocket, or nullptr if there is none. The server owns the socket as its parent; delete it when you are done, or reparent it.

See also hasPendingConnections() and newConnection().

void QTcpServer::pauseAccepting()

Pauses accepting new connections. Queued connections stay in the operating system backlog until resumeAccepting() is called.

See also resumeAccepting().

[signal] void QTcpServer::pendingConnectionAvailable()

This signal is emitted every time a connection has been added to the pending connections, by addPendingConnection(). For a QTcpServer that is when newConnection() is emitted; a QSslServer emits it once the handshake of the connection is done.

This function was introduced in 6.4.

See also hasPendingConnections() and nextPendingConnection().

void QTcpServer::resumeAccepting()

Resumes accepting new connections after pauseAccepting().

See also pauseAccepting().

QHostAddress QTcpServer::serverAddress() const

Returns the address the server is listening on, or a null address if it is not listening.

See also serverPort() and listen().

QAbstractSocket::SocketError QTcpServer::serverError() const

Returns the type of error that last occurred in the server.

See also errorString().

quint16 QTcpServer::serverPort() const

Returns the port the server is listening on, or 0 if it is not listening.

See also serverAddress() and listen().

void QTcpServer::setMaxPendingConnections(int numConnections)

Sets the maximum number of pending accepted connections to numConnections. The server stops accepting while that many connections are waiting. The default is 30.

See also maxPendingConnections() and nextPendingConnection().

bool QTcpServer::setSocketDescriptor(qintptr socketDescriptor)

Sets the server to listen on the existing operating system socket socketDescriptor, which must already be bound and listening. The descriptor is switched to non-blocking mode and the server takes ownership of it. Returns true on success; returns false if the server is already listening or the descriptor is invalid.

See also socketDescriptor() and listen().

qintptr QTcpServer::socketDescriptor() const

Returns the native operating system socket descriptor the server is listening on, or -1 if it is not listening.

See also setSocketDescriptor().

bool QTcpServer::waitForNewConnection(int msec = 0, bool *timedOut = nullptr)

Waits up to msec milliseconds for a new connection to arrive (a negative value waits forever). Returns true if a connection is available. Returns false on timeout or error; if timedOut is not null, it is set to true on a timeout.

See also hasPendingConnections() and nextPendingConnection().