QLocalServer 类提供基于本地套接字的服务器。 更多...
| 头: | #include <QLocalServer> |
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network) |
| qmake: | QT += network |
| 继承: | QObject |
| enum | SocketOption { NoOptions, UserAccessOption, GroupAccessOption, OtherAccessOption, WorldAccessOption, AbstractNamespaceOption } |
| flags | SocketOptions |
| QLocalServer (QObject * parent = nullptr) | |
| virtual | ~QLocalServer () |
| QBindable<QLocalServer::SocketOptions> | bindableSocketOptions () |
| 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 | listenBacklogSize () const |
| int | maxPendingConnections () const |
| virtual QLocalSocket * | nextPendingConnection () |
| QAbstractSocket::SocketError | serverError () const |
| QString | serverName () const |
| void | setListenBacklogSize (int size ) |
| 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 ) |
| virtual void | incomingConnection (quintptr socketDescriptor ) |
此类使接受传入本地套接字连接成为可能。
调用 listen () 让服务器按指定键开始监听传入连接。 newConnection () 信号然后被发射,每次客户端连接到服务器时。
调用 nextPendingConnection () 以接受待决连接作为已连接 QLocalSocket 。函数返回的指针指向 QLocalSocket 可以用于与客户端通信。
若出现错误, serverError () 返回错误的类型,且 errorString () 可以被调用,以获得发生什么的人类可读描述。
当监听连接时,可获得服务器正监听的名称透过 serverName ().
调用 close () 使 QLocalServer 停止监听传入连接。
尽管 QLocalServer 是为用于事件循环而设计的,没有事件循环使用它是可能的。在此情况下,必须使用 waitForNewConnection () 阻塞直到连接可用或超时到期。
另请参阅 QLocalSocket and QTcpServer .
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. On Linux and Android it is possible to use sockets with abstract addresses; socket permissions have no meaning for such sockets.
| 常量 | 值 | 描述 |
|---|---|---|
QLocalServer::NoOptions
|
0x0
|
未设置访问限定。 |
QLocalServer::UserAccessOption
|
0x01
|
访问被限定到如创建套接字的进程的相同用户。 |
QLocalServer::GroupAccessOption
|
0x2
|
在 Linux 访问被限定到相同组,除创建套接字的用户外。在 Windows 访问被限定到首要进程组 |
QLocalServer::OtherAccessOption
|
0x4
|
在 Linux 访问可用于每个人,除创建套接字的用户和组外。在 Windows 访问可用于每个人。 |
QLocalServer::WorldAccessOption
|
0x7
|
无访问限定。 |
QLocalServer::AbstractNamespaceOption
|
0x8
|
The listening socket will be created in the abstract namespace. This flag is specific to Linux. In case of other platforms, for the sake of code portability, this flag is equivalent to WorldAccessOption. |
SocketOptions 类型是 typedef 对于 QFlags <SocketOption>。它存储 SocketOption 值的 OR 组合。
另请参阅 socketOptions .
[bindable]
socketOptions
:
SocketOptions
注意: This property supports QProperty 绑定。
This property holds the socket options that control how the socket operates.
For example, the socket may restrict access to what user ids can connect to the socket.
这些选项必须被设置先于 listen () 被调用。
在某些情况下 (譬如:在 Linux 采用 Unix 域套接字),对套接字的访问将由文件系统权限确定,并基于 umask 创建。设置访问标志将覆盖这,且会限定 (或准许按指定访问)。
基于 Unix 的其它操作系统,譬如 macOS,不遵守用于 Unix 域套接字的文件权限,默认情况下拥有 WorldAccess,且这些权限标志将不起作用。
在 Windows, UserAccessOption 足以允许非提升进程连接到由相同用户运行的提升进程所创建的本地服务器。 GroupAccessOption 引用首要进程组 (见 Windows 文档编制 TokenPrimaryGroup)。 OtherAccessOption 引用众所周知的 Everyone (每个人) 组。
On Linux platforms it is possible to create a socket in the abstract namespace, which is independent of the filesystem. Using this kind of socket implies ignoring permission options. On other platforms AbstractNamespaceOption 相当于 WorldAccessOption .
默认情况下,没有设置标志,访问权限平台默认。
另请参阅 listen ().
[explicit]
QLocalServer::
QLocalServer
(
QObject
*
parent
= nullptr)
创建新的本地套接字服务器,采用给定 parent .
另请参阅 listen ().
[虚拟]
QLocalServer::
~QLocalServer
()
销毁 QLocalServer 对象。若服务器正在监听连接,它将自动关闭。
任何仍连接的客户端 QLocalSockets 必须断开连接,或重设父级在删除服务器之前。
另请参阅 close ().
停止监听传入连接。现有连接不受影响,但任何新连接将被拒绝。
另请参阅 isListening () 和 listen ().
返回人类可读消息适合当前报告错误通过 serverError ()。若没有合适字符串可用,返回空字符串。
另请参阅 serverError ().
返回服务器正监听的完整路径。
注意:这特定于平台
另请参阅 listen () 和 serverName ().
[虚拟]
bool
QLocalServer::
hasPendingConnections
() const
返回
true
若服务器有待决连接;否则返回
false
.
另请参阅 nextPendingConnection () 和 setMaxPendingConnections ().
[virtual protected]
void
QLocalServer::
incomingConnection
(
quintptr
socketDescriptor
)
此虚函数被调用由 QLocalServer 当新连接可用时。 socketDescriptor 是已接受连接的本机套接字描述符。
基实现创建 QLocalSocket ,设置套接字描述符,然后存储 QLocalSocket 在待决连接的内部列表中。最后 newConnection () 发射。
重实现此函数以更改服务器行为,当连接可用时。
另请参阅 newConnection (), nextPendingConnection (),和 QLocalSocket::setSocketDescriptor ().
返回
true
若服务器正在监听传入连接,否则 false。
告诉服务器去监听传入连接按 name 。若服务器目前在监听,那么它返回 false。当成功时返回 true,否则返回 false。
name 可以是单个名称且 QLocalServer 会确定正确的特定平台路径。 serverName () 将返回被传入 listen 的名称。
通常,只需传递像 foo 的名称,但在 Unix 也可以是如 /tmp/foo 的路径,而在 Windows 这可以是如 \\.\pipe\foo 的管道路径
注意: 在 Unix 若服务器崩溃没有关闭监听,监听将失败采用 AddressInUseError。要创建新服务器,文件应被移除。在 Windows,2 个本地服务器可以同时监听同一管道,但任何连接会转到 2 服务器之一。
另请参阅 serverName (), isListening (),和 close ().
指导服务器去监听传入连接按
socketDescriptor
。特性返回
false
若服务器目前在监听。它返回
true
当成功时;否则,它返回
false
。套接字必须就绪以接受新连接,无需调用额外特定平台函数。套接字被设为非阻塞模式。
serverName (), fullServerName () 可能返回带有名称的字符串,若平台支持此选项;否则,它们返回空 QString . In particular, the addresses of sockets in the abstract namespace supported by Linux will not yield useful names if they contain unprintable characters.
另请参阅 isListening () 和 close ().
[since 6.3]
int
QLocalServer::
listenBacklogSize
() const
Returns the backlog queue size of to be accepted connections.
This function was introduced in Qt 6.3.
另请参阅 setListenBacklogSize ().
返回最大待决已接受连接数。默认为 30。
另请参阅 setMaxPendingConnections () 和 hasPendingConnections ().
[signal]
void
QLocalServer::
newConnection
()
此信号发射,每有当新连接可用时。
另请参阅 hasPendingConnections () 和 nextPendingConnection ().
[虚拟]
QLocalSocket
*QLocalServer::
nextPendingConnection
()
返回下一待决连接作为连接的 QLocalSocket 对象。
套接字是作为服务器子级创建的,意味着会自动删除它当 QLocalServer 对象被销毁。明确删除对象仍是好主意当这样处理时,以避免浪费内存。
nullptr
被返回若在没有待决连接时调用此函数。
另请参阅 hasPendingConnections (), newConnection (),和 incomingConnection ().
[static]
bool
QLocalServer::
removeServer
(const
QString
&
name
)
移除任何服务器实例可能导致调用
listen
() 失败并返回
true
若成功;否则返回
false
。此函数意味着从崩溃恢复,当先前服务器实例尚未被清理时。
在 Windows,此函数什么都不做;在 Unix,它移除套接字文件给定通过 name .
警告: 小心避免移除正运行实例的套接字。
返回最后出现的错误类型或 NoError .
另请参阅 errorString ().
返回服务器名称若服务器正监听连接;否则返回 QString()
另请参阅 listen () 和 fullServerName ().
[since 6.3]
void
QLocalServer::
setListenBacklogSize
(
int
size
)
Sets the backlog queue size of to be accepted connections to size . The operating system might reduce or ignore this value. By default, the queue size is 50.
注意: This property must be set prior to calling listen ().
This function was introduced in Qt 6.3.
另请参阅 listenBacklogSize ().
把最大待决接受连接数设为 numConnections . QLocalServer 将接受不超过 numConnections 传入连接先于 nextPendingConnection () 被调用。
注意:即使 QLocalServer 将停止接受新连接,在达到其待决连接的最大数后,操作系统仍可能将它们保持在队列中,这会导致客户端发出已连接信号。
另请参阅 maxPendingConnections () 和 hasPendingConnections ().
返回服务器用于监听传入指令的本机套接字描述符,或 -1 若服务器未监听。
描述符类型从属平台:
另请参阅 listen ().
返回在套接字上设置的套接字选项。
注意: getter 函数对于特性 socketOptions。
另请参阅 setSocketOptions ().
等待最多
msec
毫秒或直到传入连接可用。返回
true
若连接可用;否则返回
false
。若操作超时且
timedOut
不是
nullptr
,*timedOut 将被设为 true。
这是阻塞函数调用。在单线程 GUI 应用程序中不建议使用它,由于整个应用程序将停止响应直到函数返回。waitForNewConnection() 最有用,当没有事件循环可用时。
非阻塞替代是连接到 newConnection () 信号。
若 msec 为 -1,此函数不会超时。
另请参阅 hasPendingConnections () 和 nextPendingConnection ().