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

Synopsis¶
Functions¶
def
close
()def
errorString
()def
fullServerName
()def
isListening
()def
listen
(name)def
listen
(socketDescriptor)def
maxPendingConnections
()def
newConnection
()def
serverError
()def
serverName
()def
setMaxPendingConnections
(numConnections)def
setSocketOptions
(options)def
socketDescriptor
()def
socketOptions
()def
waitForNewConnection
(msec)
Virtual functions¶
def
hasPendingConnections
()def
incomingConnection
(socketDescriptor)def
nextPendingConnection
()
Static functions¶
def
removeServer
(name)
Detailed Description¶
This class makes it possible to accept incoming local socket connections.
Call listen()
to have the server start listening for incoming connections on a specified key. The 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
-
class
PySide6.QtNetwork.
QLocalServer
([parent=None])¶ - Parameters
parent –
PySide6.QtCore.QObject
Create a new local socket server with the given parent
.
See also
-
PySide6.QtNetwork.QLocalServer.
SocketOption
¶
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.
Constant
Description
QLocalServer.NoOptions
No access restrictions have been set.
QLocalServer.UserAccessOption
Access is restricted to the same user as the process that created the socket.
QLocalServer.GroupAccessOption
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
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
No access restrictions.
See also
-
PySide6.QtNetwork.QLocalServer.
close
()¶
Stop listening for incoming connections. Existing connections are not affected, but any new connections will be refused.
See also
-
PySide6.QtNetwork.QLocalServer.
errorString
()¶ - Return type
str
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
-
PySide6.QtNetwork.QLocalServer.
fullServerName
()¶ - Return type
str
Returns the full path that the server is listening on.
Note: This is platform specific
See also
-
PySide6.QtNetwork.QLocalServer.
hasPendingConnections
()¶ - Return type
bool
Returns true
if the server has a pending connection; otherwise returns false
.
-
PySide6.QtNetwork.QLocalServer.
incomingConnection
(socketDescriptor)¶ - Parameters
socketDescriptor –
quintptr
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 is emitted.
Reimplement this function to alter the server’s behavior when a connection is available.
-
PySide6.QtNetwork.QLocalServer.
isListening
()¶ - Return type
bool
Returns true
if the server is listening for incoming connections otherwise false.
-
PySide6.QtNetwork.QLocalServer.
listen
(name)¶ - Parameters
name – str
- Return type
bool
-
PySide6.QtNetwork.QLocalServer.
listen
(socketDescriptor) - Parameters
socketDescriptor –
qintptr
- Return type
bool
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.
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
-
PySide6.QtNetwork.QLocalServer.
maxPendingConnections
()¶ - Return type
int
Returns the maximum number of pending accepted connections. The default is 30.
-
PySide6.QtNetwork.QLocalServer.
newConnection
()¶
-
PySide6.QtNetwork.QLocalServer.
nextPendingConnection
()¶ - Return type
Returns the next pending connection as a connected QLocalSocket
object.
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.
None
is returned if this function is called when there are no pending connections.
-
static
PySide6.QtNetwork.QLocalServer.
removeServer
(name)¶ - Parameters
name – str
- Return type
bool
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 function does nothing; on Unix, it removes the socket file given by name
.
Warning
Be careful to avoid removing sockets of running instances.
-
PySide6.QtNetwork.QLocalServer.
serverError
()¶ - Return type
Returns the type of error that occurred last or NoError
.
See also
-
PySide6.QtNetwork.QLocalServer.
serverName
()¶ - Return type
str
Returns the server name if the server is listening for connections; otherwise returns QString()
See also
-
PySide6.QtNetwork.QLocalServer.
setMaxPendingConnections
(numConnections)¶ - Parameters
numConnections – int
Sets the maximum number of pending accepted connections to numConnections
. QLocalServer
will accept no more than numConnections
incoming connections before nextPendingConnection()
is called.
Note: 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.
-
PySide6.QtNetwork.QLocalServer.
setSocketOptions
(options)¶ - Parameters
options –
SocketOptions
See also
-
PySide6.QtNetwork.QLocalServer.
socketDescriptor
()¶ - Return type
qintptr
Returns the native socket descriptor the server uses to listen for incoming instructions, or -1 if the server is not listening.
The type of the descriptor depends on the platform:
On Windows, the returned value is a Winsock 2 Socket Handle .
On INTEGRITY, the returned value is the
QTcpServer
socket descriptor and the type is defined bysocketDescriptor
.On all other UNIX-like operating systems, the type is a file descriptor representing a listening socket.
See also
-
PySide6.QtNetwork.QLocalServer.
socketOptions
()¶ - Return type
SocketOptions
Returns the socket options set on the socket.
See also
-
PySide6.QtNetwork.QLocalServer.
waitForNewConnection
(msec)¶ - Parameters
msec – int
- Return type
(retval, timeOut)
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 None
, *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. is mostly useful when there is no event loop available.
The non-blocking alternative is to connect to the signal.
If msec is -1, this function will not time out.
© 2021 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.