QLocalServer Class
The QLocalServer class accepts connections from other processes on the same machine. More…
| Header | #include <QLocalServer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Inherits | QObject |
List of all members, including inherited members
Public Types
| enum | SocketOption |
| typedef | SocketOptions |
Public Functions
| QLocalServer(QObject *parent = nullptr) | |
| ~QLocalServer() | |
| bool | listen(const QString &name) |
| void | close() |
| bool | isListening() const |
| QString | serverName() const |
| QString | fullServerName() const |
| QString | errorString() const |
| QLocalSocket::LocalSocketError | serverError() const |
| virtual bool | hasPendingConnections() const |
| virtual QLocalSocket * | nextPendingConnection() |
| int | maxPendingConnections() const |
| void | setMaxPendingConnections(int numConnections) |
| bool | waitForNewConnection(int msec = 0, bool *timedOut = nullptr) |
| SocketOptions | socketOptions() const |
| void | setSocketOptions(SocketOptions options) |
| qintptr | socketDescriptor() const |
Signals
| void | newConnection() |
Static Public Members
| bool | removeServer(const QString &name) |
Protected Functions
| virtual void | incomingConnection(quintptr socketDescriptor) |
Detailed Description
Call listen() with a name, then fetch each new connection when newConnection() is emitted:
QLocalServer server;
server.listen("my-service");
QObject::connect(&server, &QLocalServer::newConnection, [&] {
QLocalSocket *client = server.nextPendingConnection();
client->write("hello");
});
If a previous process crashed without closing its server, listen() fails with an address-in-use error. Call removeServer() first to clear the stale socket file.
See also QLocalSocket.
Differences from Qt
Linux and other POSIX systems only, using Unix domain sockets. There is no listening backlog setting or AbstractNamespaceOption.
The class is not built for Zephyr, where QT_NO_LOCALSERVER is defined instead. See Nt on Zephyr.
Member Type Documentation
enum QLocalServer::SocketOption
Selects who may connect to the server.
| Constant | Description |
|---|---|
QLocalServer::NoOptions | Access follows the process's umask. |
QLocalServer::UserAccessOption | The owner may connect. |
QLocalServer::GroupAccessOption | The owner's group may connect. |
QLocalServer::OtherAccessOption | Other users may connect. |
QLocalServer::WorldAccessOption | Everyone may connect. |
typedef QLocalServer::SocketOptions
The SocketOptions type is a typedef for QFlags<SocketOption>. It stores an OR combination of SocketOption values.
Member Function Documentation
QLocalServer::QLocalServer(QObject *parent = nullptr)
Constructs a local server with the given parent.
QLocalServer::~QLocalServer()
Destroys the server and stops listening. Accepted sockets still pending are deleted with it.
void QLocalServer::close()
Stops listening and removes the socket file. Connections already accepted stay open.
See also listen().
QString QLocalServer::errorString() const
Returns a description of the last error.
See also serverError().
QString QLocalServer::fullServerName() const
Returns the path of the listening socket, or an empty string if not listening.
See also serverName().
bool QLocalServer::hasPendingConnections() const
Returns true if a connection is waiting for nextPendingConnection().
void QLocalServer::incomingConnection(quintptr socketDescriptor)
Called for each accepted connection with its socketDescriptor. The default wraps it in a QLocalSocket and queues it for nextPendingConnection().
bool QLocalServer::isListening() const
Returns true if the server is listening.
See also listen().
bool QLocalServer::listen(const QString &name)
Starts listening on the socket name. A relative name is placed in QDir::tempPath(). Returns true on success.
See also close(), removeServer() and serverError().
int QLocalServer::maxPendingConnections() const
Returns the most connections held for nextPendingConnection().
See also setMaxPendingConnections().
void QLocalServer::newConnection()
This signal is emitted each time a new connection is available.
QLocalSocket * QLocalServer::nextPendingConnection()
Returns the next pending connection, or nullptr if there is none. The socket is a child of the server until you reparent or delete it.
See also newConnection().
bool QLocalServer::removeServer(const QString &name)
Removes the socket file for name, which clears the leftovers of a crashed server. Returns true if there is no longer such a file.
See also listen().
QLocalSocket::LocalSocketError QLocalServer::serverError() const
Returns the last error.
See also errorString().
QString QLocalServer::serverName() const
Returns the name given to listen(), or an empty string if not listening.
See also fullServerName().
void QLocalServer::setMaxPendingConnections(int numConnections)
Sets the most connections held for nextPendingConnection() to numConnections. Further clients wait in the system's queue.
See also maxPendingConnections().
void QLocalServer::setSocketOptions(SocketOptions options)
Sets the access options to options, which apply from the next listen().
See also socketOptions().
qintptr QLocalServer::socketDescriptor() const
Returns the native listening descriptor, or -1 if not listening.
SocketOptions QLocalServer::socketOptions() const
Returns the access options.
See also setSocketOptions().
bool QLocalServer::waitForNewConnection(int msec = 0, bool *timedOut = nullptr)
Waits up to msec milliseconds for a connection; a negative value waits forever. Returns true if one is pending. If timedOut is not null, it says whether the wait timed out.
See also hasPendingConnections().