QLocalServer 類

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 .

成員類型文檔編製

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. 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 ().

void QLocalServer:: close ()

停止監聽傳入連接。現有連接不受影響,但任何新連接將被拒絕。

另請參閱 isListening () 和 listen ().

QString QLocalServer:: errorString () const

返迴人類可讀消息適閤當前報告錯誤通過 serverError ()。若沒有閤適字符串可用,返迴空字符串。

另請參閱 serverError ().

QString QLocalServer:: fullServerName () const

返迴服務器正監聽的完整路徑。

注意:這特定於平颱

另請參閱 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 ().

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 )

指導服務器去監聽傳入連接按 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 ().

int QLocalServer:: maxPendingConnections () const

返迴最大待決已接受連接數。默認為 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 .

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

QAbstractSocket::SocketError QLocalServer:: serverError () const

返迴最後齣現的錯誤類型或 NoError .

另請參閱 errorString ().

QString QLocalServer:: serverName () const

返迴服務器名稱若服務器正監聽連接;否則返迴 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 ().

void QLocalServer:: setMaxPendingConnections ( int numConnections )

把最大待決接受連接數設為 numConnections . QLocalServer 將接受不超過 numConnections 傳入連接先於 nextPendingConnection () 被調用。

注意:即使 QLocalServer 將停止接受新連接,在達到其待決連接的最大數後,操作係統仍可能將它們保持在隊列中,這會導緻客戶端發齣已連接信號。

另請參閱 maxPendingConnections () 和 hasPendingConnections ().

qintptr QLocalServer:: socketDescriptor () const

返迴服務器用於監聽傳入指令的本機套接字描述符,或 -1 若服務器未監聽。

描述符類型從屬平颱:

另請參閱 listen ().

QLocalServer::SocketOptions QLocalServer:: socketOptions () const

返迴在套接字上設置的套接字選項。

注意: getter 函數對於特性 socketOptions。

另請參閱 setSocketOptions ().

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

等待最多 msec 毫秒或直到傳入連接可用。返迴 true 若連接可用;否則返迴 false 。若操作超時且 timedOut 不是 nullptr ,*timedOut 將被設為 true。

這是阻塞函數調用。在單綫程 GUI 應用程序中不建議使用它,由於整個應用程序將停止響應直到函數返迴。waitForNewConnection() 最有用,當沒有事件循環可用時。

非阻塞替代是連接到 newConnection () 信號。

若 msec 為 -1,此函數不會超時。

另請參閱 hasPendingConnections () 和 nextPendingConnection ().