QLocalServer 类

QLocalServer class provides a local socket based server. 更多...

头: #include <QLocalServer>
qmake: QT += network
Since: Qt 4.4
继承: QObject

公共类型

enum SocketOption { NoOptions, UserAccessOption, GroupAccessOption, OtherAccessOption, WorldAccessOption }
flags SocketOptions

特性

公共函数

QLocalServer (QObject * parent = nullptr)
virtual ~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 QLocalSocket * nextPendingConnection ()
QAbstractSocket::SocketError serverError () const
QString serverName () const
void setMaxPendingConnections (int numConnections )
void setSocketOptions (QLocalServer::SocketOptions options )
qintptr socketDescriptor () const
QLocalServer::SocketOptions socketOptions () const
bool waitForNewConnection (int msec = 0, bool * timedOut = nullptr)

信号

void newConnection ()

静态公共成员

bool removeServer (const QString & name )
const QMetaObject staticMetaObject

保护函数

virtual void incomingConnection (quintptr socketDescriptor )

额外继承成员

详细描述

QLocalServer class provides a local socket based server.

此类使接受传入本地套接字连接成为可能。

调用 listen () 让服务器按指定键开始监听传入连接。 newConnection () 信号然后被发射,每次客户端连接到服务器时。

调用 nextPendingConnection () 以接受待决连接作为已连接 QLocalSocket 。函数返回的指针指向 QLocalSocket 可用于与客户端通信。

若出现错误, serverError () 返回错误的类型,且 errorString () 可以被调用,以获得发生什么的人类可读描述。

当监听连接时,可获得服务器正监听的名称透过 serverName ().

调用 close () 使 QLocalServer 停止监听传入连接。

尽管 QLocalServer is designed for use with an event loop, it's possible to use it without one. In that case, you must use waitForNewConnection () 阻塞直到连接可用或超时到期。

另请参阅 QLocalSocket and QTcpServer .

成员类型文档编制

enum QLocalServer:: SocketOption
flags QLocalServer:: SocketOptions

此枚举描述可用于创建套接字的可能选项。这会改变支持套接字访问权限的平台 (Linux、Windows) 访问权限。从属平台,GroupAccess 和 OtherAccess 的含义可能略有不同。

常量 描述
QLocalServer::NoOptions 0x0 无访问限定已设置。
QLocalServer::UserAccessOption 0x01 访问被限定到如创建套接字的进程的相同用户。
QLocalServer::GroupAccessOption 0x2 在 Linux 访问被限定到相同组,除创建套接字的用户外。在 Windows 访问被限定到首要进程组
QLocalServer::OtherAccessOption 0x4 在 Linux 访问可用于每个人,除创建套接字的用户和组外。在 Windows 访问可用于每个人。
QLocalServer::WorldAccessOption 0x7 无访问限定。

该枚举在 Qt 5.0 引入或被修改。

SocketOptions 类型是 typedef 对于 QFlags <SocketOption>。它存储 SocketOption 值的 OR 组合。

另请参阅 socketOptions .

特性文档编制

socketOptions : SocketOptions

setSocketOptions 方法控制如何操作套接字。例如,套接字可能限定哪些用户 ID 可以连接到套接字的访问。

这些选项必须被设置先于 listen () 被调用。

在某些情况下,譬如在 Linux 的 Unix 域套接字,对套接字的访问将由文件系统权限决定,并基于 umask 创建。设置访问标志将覆盖此,并按指定限定访问或准许访问。

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.

在 Windows, UserAccessOption 足以允许非提升进程连接到由相同用户运行的提升进程所创建的本地服务器。 GroupAccessOption 引用首要进程组 (见 Windows 文档编制 TokenPrimaryGroup)。 OtherAccessOption 引用众所周知的 Everyone (每个人) 组。

默认情况下,没有设置标志,访问权限平台默认。

该特性在 Qt 5.0 引入。

访问函数:

QLocalServer::SocketOptions socketOptions () const
void setSocketOptions (QLocalServer::SocketOptions options )

另请参阅 listen ().

成员函数文档编制

QLocalServer:: QLocalServer ( QObject * parent = nullptr)

创建新的本地套接字服务器,采用给定 parent .

另请参阅 listen ().

[virtual] QLocalServer:: ~QLocalServer ()

销毁 QLocalServer 对象。若服务器正在监听连接,它将自动关闭。

任何仍连接的客户端 QLocalSockets 必须断开连接,或重设父级在删除服务器之前。

另请参阅 close ().

void QLocalServer:: close ()

停止监听传入连接。现有连接不受影响,但任何新连接将被拒绝。

另请参阅 isListening () 和 listen ().

QString QLocalServer:: errorString () const

返回人类可读消息适合当前报告错误通过 serverError ()。若没有合适字符串可用,返回空字符串。

另请参阅 serverError ().

QString QLocalServer:: fullServerName () const

返回服务器正监听的完整路径。

注意:这特定于平台

另请参阅 listen () 和 serverName ().

[virtual] bool QLocalServer:: hasPendingConnections () const

返回 true 若服务器有待决连接;否则返回 false .

另请参阅 nextPendingConnection () 和 setMaxPendingConnections ().

[virtual protected] void QLocalServer:: incomingConnection ( quintptr socketDescriptor )

此虚函数被调用由 QLocalServer 当新连接可用时。 socketDescriptor is the native socket descriptor for the accepted connection.

基实现创建 QLocalSocket ,设置套接字描述符,然后存储 QLocalSocket 在待决连接的内部列表中。最后 newConnection () 被发射。

重实现此函数以更改服务器行为,当连接可用时。

另请参阅 newConnection (), nextPendingConnection (),和 QLocalSocket::setSocketDescriptor ().

bool QLocalServer:: isListening () const

返回 true 若服务器正在监听传入连接,否则 false。

另请参阅 listen () 和 close ().

bool QLocalServer:: listen (const QString & name )

告诉服务器去监听传入连接在 name 。若服务器目前正在监听,那么它会返回 false。在成功时返回 true,否则返回 false。

name 可以是单个名称且 QLocalServer 会确定正确的特定平台路径。 serverName () 将返回被传入 listen 的名称。

通常,只需传递像 foo 的名称,但在 Unix 也可以是如 /tmp/foo 的路径,而在 Windows 这可以是如 \\.\pipe\foo 的管道路径

注意: 在 Unix 若服务器崩溃没有关闭监听,监听将失败采用 AddressInUseError。要创建新服务器,文件应被移除。在 Windows,2 个本地服务器可以同时监听同一管道,但任何连接会转到 2 服务器之一。

另请参阅 serverName (), isListening (),和 close ().

bool QLocalServer:: listen ( qintptr socketDescriptor )

Instructs the server to listen for incoming connections on socketDescriptor 。特性返回 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 .

该函数在 Qt 5.0 引入。

另请参阅 isListening () 和 close ().

int QLocalServer:: maxPendingConnections () const

返回最大待决已接受连接数。默认为 30。

另请参阅 setMaxPendingConnections () 和 hasPendingConnections ().

[signal] void QLocalServer:: newConnection ()

此信号被发射,每次有新连接可用时。

另请参阅 hasPendingConnections () 和 nextPendingConnection ().

[virtual] QLocalSocket *QLocalServer:: nextPendingConnection ()

返回下一待决连接作为已连接 QLocalSocket 对象。

套接字是作为服务器子级创建的,意味着它会被自动删除当 QLocalServer 对象被销毁。当采用完成后明确删除对象仍是好主意,以避免浪费内存。

返回 0 若在没有待决连接时调用此函数。

另请参阅 hasPendingConnections (), newConnection (),和 incomingConnection ().

[static] bool QLocalServer:: removeServer (const QString & name )

移除任何服务器实例可能导致调用 listen () 失败并返回 true 若成功;否则返回 false 。此函数意味着从崩溃恢复,当先前服务器实例尚未被清理时。

在 Windows,此函数什么都不做;在 Unix,它移除套接字文件给定通过 name .

警告: 小心避免移除正运行实例的套接字。

该函数在 Qt 4.5 引入。

QAbstractSocket::SocketError QLocalServer:: serverError () const

返回最后发生的错误类型或 NoError .

另请参阅 errorString ().

QString QLocalServer:: serverName () const

返回服务器名称若服务器正监听连接;否则返回 QString()

另请参阅 listen () 和 fullServerName ().

void QLocalServer:: setMaxPendingConnections ( int numConnections )

把最大待决接受连接数设为 numConnections . QLocalServer 将接受不超过 numConnections 传入连接先于 nextPendingConnection () 被调用。

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.

另请参阅 maxPendingConnections () 和 hasPendingConnections ().

qintptr QLocalServer:: socketDescriptor () const

返回服务器用于监听传入指令的本机套接字描述符,或 -1 若服务器未监听。

描述符类型从属平台:

该函数在 Qt 5.10 引入。

另请参阅 listen ().

QLocalServer::SocketOptions QLocalServer:: socketOptions () const

返回在套接字上设置的套接字选项。

该函数在 Qt 5.0 引入。

注意: Getter 函数对于特性 socketOptions .

另请参阅 setSocketOptions ().

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

等待最多 msec 毫秒或直到传入连接可用。返回 true 若连接可用;否则返回 false 。若操作超时且 timedOut is not 0, *timedOut will be set to true.

这是阻塞函数调用。在单线程 GUI 应用程序中不建议使用它,由于整个应用程序将停止响应直到函数返回。waitForNewConnection() 最有用,当没有事件循环可用时。

非阻塞替代是连接到 newConnection () 信号。

若 msec 为 -1,此函数不会超时。

另请参阅 hasPendingConnections () 和 nextPendingConnection ().