Movatterモバイル変換


[0]ホーム

URL:


Back to Qt.io
Contact UsBlogDownload Qt
Qt documentation

QLocalServer Class

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

Header:#include <QLocalServer>
qmake: QT += network
Since: Qt 4.4
Inherits:QObject

This class was introduced in Qt 4.4.

Public Types

enumSocketOption { NoOptions, UserAccessOption, GroupAccessOption, OtherAccessOption, WorldAccessOption }
flagsSocketOptions

Properties

Public Functions

QLocalServer(QObject *parent = nullptr)
virtual~QLocalServer()
voidclose()
QStringerrorString() const
QStringfullServerName() const
virtual boolhasPendingConnections() const
boolisListening() const
boollisten(const QString &name)
boollisten(qintptrsocketDescriptor)
intmaxPendingConnections() const
virtual QLocalSocket *nextPendingConnection()
QAbstractSocket::SocketErrorserverError() const
QStringserverName() const
voidsetMaxPendingConnections(intnumConnections)
voidsetSocketOptions(QLocalServer::SocketOptionsoptions)
qintptrsocketDescriptor() const
QLocalServer::SocketOptionssocketOptions() const
boolwaitForNewConnection(intmsec = 0, bool *timedOut = nullptr)

Signals

Static Public Members

boolremoveServer(const QString &name)

Protected Functions

virtual voidincomingConnection(quintptrsocketDescriptor)

Detailed Description

This class makes it possible to accept incoming local socket connections.

Calllisten() to have the server start listening for incoming connections on a specified key. ThenewConnection() signal is then emitted each time a client connects to the server.

CallnextPendingConnection() to accept the pending connection as a connectedQLocalSocket. The function returns a pointer to aQLocalSocket that can be used for communicating with the client.

If an error occurs,serverError() returns the type of error, anderrorString() 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 throughserverName().

Callingclose() 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 usewaitForNewConnection(), which blocks until either a connection is available or a timeout expires.

See alsoQLocalSocket andQTcpServer.

Member Type Documentation

enum QLocalServer::SocketOption
flags QLocalServer::SocketOptions

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::NoOptions0x0No access restrictions have been set.
QLocalServer::UserAccessOption0x01Access is restricted to the same user as the process that created the socket.
QLocalServer::GroupAccessOption0x2Access 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::OtherAccessOption0x4Access is available to everyone but the user and group that created the socket on Linux. Access is available to everyone on Windows.
QLocalServer::WorldAccessOption0x7No access restrictions.

This enum was introduced or modified in Qt 5.0.

The SocketOptions type is a typedef forQFlags<SocketOption>. It stores an OR combination of SocketOption values.

See alsosocketOptions.

Property Documentation

socketOptions :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 beforelisten() 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 macOS, 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.

This property was introduced in Qt 5.0.

Access functions:

QLocalServer::SocketOptionssocketOptions() const
voidsetSocketOptions(QLocalServer::SocketOptionsoptions)

See alsolisten().

Member Function Documentation

QLocalServer::QLocalServer(QObject *parent = nullptr)

Create a new local socket server with the givenparent.

See alsolisten().

[signal]void QLocalServer::newConnection()

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

See alsohasPendingConnections() andnextPendingConnection().

[virtual]QLocalServer::~QLocalServer()

Destroys theQLocalServer 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 alsoclose().

void QLocalServer::close()

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

See alsoisListening() andlisten().

QString QLocalServer::errorString() const

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

See alsoserverError().

QString QLocalServer::fullServerName() const

Returns the full path that the server is listening on.

Note: This is platform specific

See alsolisten() andserverName().

[virtual]bool QLocalServer::hasPendingConnections() const

Returnstrue if the server has a pending connection; otherwise returnsfalse.

See alsonextPendingConnection() andsetMaxPendingConnections().

[virtual protected]void QLocalServer::incomingConnection(quintptrsocketDescriptor)

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

The base implementation creates aQLocalSocket, sets the socket descriptor and then stores theQLocalSocket in an internal list of pending connections. FinallynewConnection() is emitted.

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

See alsonewConnection(),nextPendingConnection(), andQLocalSocket::setSocketDescriptor().

bool QLocalServer::isListening() const

Returnstrue if the server is listening for incoming connections otherwise false.

See alsolisten() andclose().

bool QLocalServer::listen(constQString &name)

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

name can be a single name andQLocalServer 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"

Note: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 alsoserverName(),isListening(), andclose().

bool QLocalServer::listen(qintptrsocketDescriptor)

Instructs the server to listen for incoming connections onsocketDescriptor. The property returnsfalse if the server is currently listening. It returnstrue on success; otherwise, it returnsfalse. 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 emptyQString.

This function was introduced in Qt 5.0.

See alsoisListening() andclose().

int QLocalServer::maxPendingConnections() const

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

See alsosetMaxPendingConnections() andhasPendingConnections().

[virtual]QLocalSocket *QLocalServer::nextPendingConnection()

Returns the next pending connection as a connectedQLocalSocket object.

The socket is created as a child of the server, which means that it is automatically deleted when theQLocalServer 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 alsohasPendingConnections(),newConnection(), andincomingConnection().

[static]bool QLocalServer::removeServer(constQString &name)

Removes any server instance that might cause a call tolisten() to fail and returnstrue if successful; otherwise returnsfalse. 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 byname.

Warning:Be careful to avoid removing sockets of running instances.

This function was introduced in Qt 4.5.

QAbstractSocket::SocketError QLocalServer::serverError() const

Returns the type of error that occurred last orNoError.

See alsoerrorString().

QString QLocalServer::serverName() const

Returns the server name if the server is listening for connections; otherwise returns QString()

See alsolisten() andfullServerName().

void QLocalServer::setMaxPendingConnections(intnumConnections)

Sets the maximum number of pending accepted connections tonumConnections.QLocalServer will accept no more thannumConnections incoming connections beforenextPendingConnection() is called.

Note: Even thoughQLocalServer 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 alsomaxPendingConnections() andhasPendingConnections().

qintptr QLocalServer::socketDescriptor() const

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 aWinsock 2 Socket Handle.
  • With WinRT and on INTEGRITY, the returned value is theQTcpServer 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.

This function was introduced in Qt 5.10.

See alsolisten().

QLocalServer::SocketOptions QLocalServer::socketOptions() const

Returns the socket options set on the socket.

Note:Getter function for property socketOptions.

This function was introduced in Qt 5.0.

See alsosetSocketOptions().

bool QLocalServer::waitForNewConnection(intmsec = 0,bool *timedOut = nullptr)

Waits for at mostmsec milliseconds or until an incoming connection is available. Returnstrue if a connection is available; otherwise returnsfalse. If the operation timed out andtimedOut is notnullptr, *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 thenewConnection() signal.

If msec is -1, this function will not time out.

See alsohasPendingConnections() andnextPendingConnection().

© 2024 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 theGNU 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.

Contents

Qt Group includes The Qt Company Oy and its global subsidiaries and affiliates.


[8]ページ先頭

©2009-2025 Movatter.jp