The QTcpServer class provides a TCP-based server. More...
|CMake:|| find_package(Qt6 REQUIRED COMPONENTS Network) |
target_link_libraries(mytarget PRIVATE Qt6::Network)
|qmake:||QT += network|
Note: All functions in this class are reentrant.
|QTcpServer(QObject *parent = nullptr)|
|virtual bool||hasPendingConnections() const|
|bool||listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)|
|virtual QTcpSocket *||nextPendingConnection()|
|void||setProxy(const QNetworkProxy &networkProxy)|
|bool||waitForNewConnection(int msec = 0, bool *timedOut = nullptr)|
|virtual void||incomingConnection(qintptr socketDescriptor)|
This class makes it possible to accept incoming TCP connections. You can specify the port or have QTcpServer pick one automatically. You can listen on a specific address or on all the machine's addresses.
Call listen() to have the server listen for incoming connections. The newConnection() signal is then emitted each time a client connects to the server. When the client connection has been added to the pending connection queue using the addPendingConnection() function, the pendingConnectionAvailable() signal is emitted.
Call nextPendingConnection() to accept the pending connection as a connected QTcpSocket. The function returns a pointer to a QTcpSocket in QAbstractSocket::ConnectedState that you can use for communicating with the client.
Calling close() makes QTcpServer stop listening for incoming connections.
Although QTcpServer is mostly designed for use with an event loop, it's possible to use it without one. In that case, you must use waitForNewConnection(), which blocks until either a connection is available or a timeout expires.
Member Function Documentation
[explicit] QTcpServer::QTcpServer(QObject *parent = nullptr)
Constructs a QTcpServer object.
parent is passed to the QObject constructor.
Destroys the QTcpServer object. If the server is listening for connections, the socket is automatically closed.
Any client QTcpSockets that are still connected must either disconnect or be reparented before the server is deleted.
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 that occurred.
[protected] void QTcpServer::addPendingConnection(QTcpSocket *socket)
This function is called by QTcpServer::incomingConnection() to add the socket to the list of pending incoming connections.
Note: Don't forget to call this member from reimplemented incomingConnection() if you do not want to break the Pending Connections mechanism. This function emits the pendingConnectionAvailable() signal after the socket has been added.
Closes the server. The server will no longer listen for incoming connections.
See also listen().
QString QTcpServer::errorString() const
Returns a human readable description of the last error that occurred.
See also serverError().
true if the server has a pending connection; otherwise returns
[virtual protected] void QTcpServer::incomingConnection(qintptr socketDescriptor)
This virtual function is called by QTcpServer when a new connection is available. The socketDescriptor argument is the native socket descriptor for the accepted connection.
Reimplement this function to alter the server's behavior when a connection is available.
Note: If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection().
Note: If you want to handle an incoming connection as a new QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the QTcpSocket object there and use its setSocketDescriptor() method.
true if the server is currently listening for incoming connections; otherwise returns
See also listen().
bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)
Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosen automatically. If address is QHostAddress::Any, the server will listen on all network interfaces.
true on success; otherwise returns
See also isListening().
Returns the backlog queue size of to be accepted connections.
This function was introduced in Qt 6.3.
See also setListenBacklogSize().
Returns the maximum number of pending accepted connections. The default is 30.
This signal is emitted every time a new connection is available, regardless of whether it has been added to the pending connections queue or not.
[virtual] QTcpSocket *QTcpServer::nextPendingConnection()
Returns the next pending connection as a connected QTcpSocket object.
The socket is created as a child of the server, which means that it is automatically deleted when the QTcpServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.
nullptr is returned if this function is called when there are no pending connections.
See also hasPendingConnections().
Pauses accepting new connections. Queued connections will remain in queue.
See also resumeAccepting().
This signal is emitted every time a new connection has been added to the pending connections queue.
Note: This is a private signal. It can be used in signal connections but cannot be emitted by the user.
This function was introduced in Qt 6.4.
QNetworkProxy QTcpServer::proxy() const
Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used.
Resumes accepting new connections.
See also pauseAccepting().
QHostAddress QTcpServer::serverAddress() const
Returns the server's address if the server is listening for connections; otherwise returns QHostAddress::Null.
QAbstractSocket::SocketError QTcpServer::serverError() const
Returns an error code for the last error that occurred.
See also errorString().
quint16 QTcpServer::serverPort() const
Returns the server's port if the server is listening for connections; otherwise returns 0.
Sets the backlog queue size of to be accepted connections to size. The operating system might reduce or ignore this value. By default, the queue size is 50.
Note: This property must be set prior to calling listen().
This function was introduced in Qt 6.3.
See also listenBacklogSize().
Sets the maximum number of pending accepted connections to numConnections. QTcpServer will accept no more than numConnections incoming connections before nextPendingConnection() is called. By default, the limit is 30 pending connections.
Clients may still able to connect after the server has reached its maximum number of pending connections (i.e., QTcpSocket can still emit the connected() signal). QTcpServer will stop accepting the new connections, but the operating system may still keep them in queue.
void QTcpServer::setProxy(const QNetworkProxy &networkProxy)
Sets the explicit network proxy for this socket to networkProxy.
To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy proxy type:
bool QTcpServer::setSocketDescriptor(qintptr socketDescriptor)
Sets the socket descriptor this server should use when listening for incoming connections to socketDescriptor. Returns
true if the socket is set successfully; otherwise returns
The socket is assumed to be in listening state.
qintptr QTcpServer::socketDescriptor() const
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.
If the server is using QNetworkProxy, the returned descriptor may not be usable with native socket functions.
Waits for at most msec milliseconds or until an incoming connection is available. Returns
true if a connection is available; otherwise returns
false. If the operation timed out and timedOut is not
nullptr, *timedOut will be set to true.
This is a blocking function call. Its use is disadvised in a single-threaded GUI application, since the whole application will stop responding until the function returns. waitForNewConnection() is mostly useful when there is no event loop available.
The non-blocking alternative is to connect to the newConnection() signal.
If msec is -1, this function will not time out.
© 2023 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.