CopperSpice API  1.9.1
QLocalServer Class Reference

The QLocalServer class provides a local socket based server. More...

Inheritance diagram for QLocalServer:
QObject

Public Typedefs

using SocketOptions = QFlags< SocketOption >
 

Public Types

enum  SocketOption
 

Public Signals

void newConnection ()
 
- Public Signals inherited from QObject
void destroyed (QObject *obj=nullptr)
 
void objectNameChanged (const QString &objectName)
 

Public Methods

 QLocalServer (QObject *parent=nullptr)
 
 ~QLocalServer ()
 
void close ()
 
QString errorString () const
 
QString fullServerName () const
 
virtual bool hasPendingConnections () const
 
bool isListening () const
 
bool listen (const QString &name)
 
bool listen (qintptr socketDescriptor)
 
int maxPendingConnections () const
 
virtual QLocalSocketnextPendingConnection ()
 
QAbstractSocket::SocketError serverError () const
 
QString serverName () const
 
void setMaxPendingConnections (int numConnections)
 
void setSocketOptions (SocketOptions options)
 
SocketOptions socketOptions () const
 
bool waitForNewConnection (int msec=0, bool *timedOut=nullptr)
 
- Public Methods inherited from QObject
 QObject (QObject *parent=nullptr)
 
 ~QObject ()
 
bool blockSignals (bool block)
 
const QList< QObject * > & children () const
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool connect (const QObject *sender, const QString &signalMethod, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
bool disconnect (const QObject *receiver, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod, const QString &location, const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
bool disconnect (const QString &signalMethod=QString (), const QObject *receiver=nullptr, const QString &slotMethod=QString ()) const
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QStringdynamicPropertyNames () const
 
virtual bool event (QEvent *event)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
template<typename T >
findChild (const QString &childName=QString ()) const
 
template<class T >
QList< T > findChildren (const QRegularExpression &regExp, Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
template<class T >
QList< T > findChildren (const QString &childName=QString (), Qt::FindChildOptions options=Qt::FindChildrenRecursively) const
 
bool inherits (const QString &className) const
 
void installEventFilter (QObject *filterObj)
 
bool isWidgetType () const
 
bool isWindowType () const
 
void killTimer (int id)
 
const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
QString objectName () const
 
QObject * parent () const
 
template<class T = QVariant>
property (const QString &name) const
 
void removeEventFilter (QObject *obj)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const QString &name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval, Qt::TimerType timerType=Qt::CoarseTimer)
 
QThreadthread () const
 

Static Public Methods

static bool removeServer (const QString &name)
 
- Static Public Methods inherited from QObject
static bool connect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool connect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection, const QString &location=QString ())
 
static bool connect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod, Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...), Qt::ConnectionType type=Qt::AutoConnection)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool connect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotLambda, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const QMetaMethod &signalMethod, const QObject *receiver, const QMetaMethod &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, const QString &signalMethod, const QString &location, const QObject *receiver, const QString &slotMethod)
 
static bool disconnect (const QObject *sender, std::nullptr_t, const QObject *receiver, std::nullptr_t)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class SlotClass , class... SlotArgs, class SlotReturn >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, SlotReturn (SlotClass::*slotMethod)(SlotArgs...))
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, std::nullptr_t slotMethod=nullptr)
 
template<class Sender , class SignalClass , class... SignalArgs, class Receiver , class T >
static bool disconnect (const Sender *sender, void (SignalClass::*signalMethod)(SignalArgs...), const Receiver *receiver, T slotMethod)
 
static QMetaObjectstaticMetaObject ()
 
static QString tr (const char *text, const char *comment=nullptr, std::optional< int > numArg=std::optional< int >())
 

Protected Methods

virtual void incomingConnection (qintptr socketDescriptor)
 
- Protected Methods inherited from QObject
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const QMetaMethod &signalMethod) const
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const QMetaMethod &signalMethod) const
 
bool isSignalConnected (const QMetaMethod &signalMethod) const
 
int receivers (const QString &signal) const
 
QObject * sender () const
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *event)
 

Properties

 socketOptions
 
- Properties inherited from QObject
 objectName
 

Additional Inherited Members

- Public Slots inherited from QObject
void deleteLater ()
 

Detailed Description

The QLocalServer class provides a local socket based server to accept incoming local socket connections. Call listen() to have the server start listening for incoming connections on a specified key. The newConnection() signal is then emitted each time a client connects to the server.

Call nextPendingConnection() to accept the pending connection as a connected QLocalSocket. The function returns a pointer to a QLocalSocket that can be used for communicating with the client.

If an error occurs, serverError() returns the type of error, and errorString() can be called to get a human readable description of what happened.

When listening for connections, the name which the server is listening on is available through serverName().

Calling close() makes QLocalServer stop listening for incoming connections.

Although QLocalServer is 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.

See also
QLocalSocket, QTcpServer

Member Typedef Documentation

Member Enumeration Documentation

This enum describes the possible options that can be used to create the socket. This changes the access permissions on platforms (Linux, Windows) that support access permissions on the socket. Both GroupAccess and OtherAccess may vary slightly in meanings depending on the platform.

ConstantValueDescription
QLocalServer:NoOptions 0x00 No access restrictions have been set
QLocalServer:UserAccessOption 0x01 Access is restricted to the same user as the process that created the socket
QLocalServer:GroupAccessOption 0x02 Access is restricted to the same group but not the user that created the socket on Linux.
Access is restricted to the primary group of the process on Windows
QLocalServer:OtherAccessOption 0x04 Access is available to everyone but the user and group that created the socket on Linux.
Access is available to everyone on Windows.
QLocalServer:WorldAccessOption 0x07 No access restrictions

Constructor & Destructor Documentation

QLocalServer::QLocalServer ( QObject parent = nullptr)
explicit

Create a new local socket server with the given parent.

See also
listen()
QLocalServer::~QLocalServer ( )

Destroys the QLocalServer object. If the server is listening for connections, it is automatically closed.

Any client QLocalSockets that are still connected must either disconnect or be reparented before the server is deleted.

See also
close()

Method Documentation

void QLocalServer::close ( )

Stop listening for incoming connections. Existing connections are not effected, but any new connections will be refused.

See also
isListening(), listen()
QString QLocalServer::errorString ( ) const

Returns the human readable message appropriate to the current error reported by serverError(). If no suitable string is available, an empty string is returned.

See also
serverError()
QString QLocalServer::fullServerName ( ) const

Returns the full path that the server is listening on.

This method is platform specific

See also
listen(), serverName()
bool QLocalServer::hasPendingConnections ( ) const
virtual

Returns true if the server has a pending connection, otherwise returns false.

See also
nextPendingConnection(), setMaxPendingConnections()
void QLocalServer::incomingConnection ( qintptr  socketDescriptor)
protectedvirtual

This virtual function is called by QLocalServer when a new connection is available. socketDescriptor is the native socket descriptor for the accepted connection.

The base implementation creates a QLocalSocket, sets the socket descriptor and then stores the QLocalSocket in an internal list of pending connections. Finally newConnection() is emitted.

Reimplement this function to alter the server's behavior when a connection is available.

See also
newConnection(), nextPendingConnection(), QLocalSocket::setSocketDescriptor()
bool QLocalServer::isListening ( ) const

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

See also
close(), listen()
bool QLocalServer::listen ( const QString name)

Tells the server to listen for incoming connections on name. If the server is currently listening then it will return false. Return true on success otherwise false.

name can be a single name and QLocalServer will determine the correct platform specific path. serverName() will return the name that is passed into listen.

Usually you would just pass in a name like "foo" but on Unix this could also be a path such as "/tmp/foo" and on Windows this could be a pipe path such as "\\.\\pipe\\foo".

On Unix if the server crashes without closing listen will fail with AddressInUseError. To create a new server the file should be removed. On Windows two local servers can listen to the same pipe at the same time, but any connections will go to one of the server.

See also
close(), isListening(), serverName()
bool QLocalServer::listen ( qintptr  socketDescriptor)

Instructs the server to listen for incoming connections on socketDescriptor. The property returns false if the server is currently listening. It returns true on success, otherwise it returns false. The socket must be ready to accept new connections with no extra platform-specific functions called. The socket is set into non-blocking mode.

The methods serverName(), fullServerName() may return a string with a name if this option is supported by the platform, otherwise, they return an empty QString.

See also
close(), isListening()
int QLocalServer::maxPendingConnections ( ) const

Returns the maximum number of pending accepted connections. The default is 30.

See also
hasPendingConnections(), setMaxPendingConnections()
void QLocalServer::newConnection ( )
signal

This signal is emitted every time a new connection is available.

See also
hasPendingConnections(), nextPendingConnection()
QLocalSocket * QLocalServer::nextPendingConnection ( )
virtual

Returns the next pending connection as a connected QLocalSocket object. Returns 0 if this method is called when there are no pending connections.

The socket is created as a child of the server, which means that it is automatically deleted when the QLocalServer object is destroyed. It is still a good idea to delete the object explicitly when you are done with it, to avoid wasting memory.

See also
hasPendingConnections(), newConnection(), incomingConnection()
bool QLocalServer::removeServer ( const QString name)
static

Removes any server instance that might cause a call to listen() to fail and returns true if successful, otherwise returns false. This function is meant to recover from a crash, when the previous server instance has not been cleaned up.

On Windows this method does nothing. On Unix it removes the socket file given by name.

Warning
Be careful to avoid removing sockets of running instances.
QAbstractSocket::SocketError QLocalServer::serverError ( ) const

Returns the type of error that occurred last or NoError.

See also
errorString()
QString QLocalServer::serverName ( ) const

Returns the server name if the server is listening for connections, otherwise returns QString.

See also
listen(), fullServerName()
void QLocalServer::setMaxPendingConnections ( int  numConnections)

Sets the maximum number of pending accepted connections to numConnections. QLocalServer will accept no more than numConnections incoming connections before nextPendingConnection() is called.

Even though QLocalServer will stop accepting new connections after it has reached its maximum number of pending connections, the operating system may still keep them in queue which will result in clients signaling that it is connected.

See also
maxPendingConnections(), hasPendingConnections()
void QLocalServer::setSocketOptions ( SocketOptions  options)

Set the socket options on the socket.

See also
QLocalServer::socketOptions
QLocalServer::SocketOptions QLocalServer::socketOptions ( ) const

Returns the socket options set on the socket.

See also
QLocalServer::socketOptions
bool QLocalServer::waitForNewConnection ( int  msec = 0,
bool *  timedOut = nullptr 
)

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 0, *timedOut will be set to true.

This is a blocking function call. Its use is ill-advised 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.

See also
hasPendingConnections(), nextPendingConnection()

Property Documentation

QLocalServer::socketOptions

The setSocketOptions method controls how the socket operates. For example the socket may restrict access to what user ids can connect to the socket.

These options must be set before listen() is called.

In some cases, such as with Unix domain sockets on Linux, the access to the socket will be determined by file system permissions, and are created based on the umask. Setting the access flags will override this and will restrict or permit access as specified.

Other Unix-based operating systems, such as OS X, do not honor file permissions for Unix domain sockets and by default have WorldAccess and these permission flags will have no effect.

On Windows, UserAccessOption is sufficient to allow a non elevated process to connect to a local server created by an elevated process run by the same user. GroupAccessOption refers to the primary group of the process (see TokenPrimaryGroup in the Windows documentation). OtherAccessOption refers to the well known "Everyone" group.

By default none of the flags are set, access permissions are the platform default.

See also
listen()
PropertiesClass Methods
read socketOptions
write setSocketOptions