The QLocalServer class provides a local socket based server. 更多...
| 頭: | #include <QLocalServer> |
| Since: | Qt 4.4 |
| 繼承: | QObject |
| QLocalServer (QObject * parent = 0) | |
| ~QLocalServer () | |
| void | close () |
| QString | errorString () const |
| QString | fullServerName () const |
| virtual bool | hasPendingConnections () const |
| bool | isListening () const |
| bool | listen (const QString & name ) |
| int | maxPendingConnections () const |
| virtual QLocalSocket * | nextPendingConnection () |
| QAbstractSocket::SocketError | serverError () const |
| QString | serverName () const |
| void | setMaxPendingConnections (int numConnections ) |
| bool | waitForNewConnection (int msec = 0, bool * timedOut = 0) |
| void | newConnection () |
| bool | removeServer (const QString & name ) |
| virtual void | incomingConnection (quintptr socketDescriptor ) |
The QLocalServer class provides a local socket based server.
此類使接受傳入本地套接字連接成為可能。
調用 listen () 讓服務器按指定鍵開始監聽傳入連接。 newConnection () 信號然後被發射,每次客戶端連接到服務器時。
調用 nextPendingConnection () 以接受待決連接作為已連接 QLocalSocket 。函數返迴的指針指嚮 QLocalSocket 可以用於與客戶端通信。
若齣現錯誤, serverError () 返迴錯誤的類型,且 errorString () 可以被調用,以獲得發生什麼的人類可讀描述。
當監聽連接時,可獲得服務器正監聽的名稱透過 serverName ().
調用 close () 使 QLocalServer 停止監聽傳入連接。
盡管 QLocalServer 是為用於事件循環而設計的,沒有事件循環使用它是可能的。在此情況下,必須使用 waitForNewConnection () 阻塞直到連接可用或超時到期。
另請參閱 QLocalSocket and QTcpServer .
創建新的本地套接字服務器,采用給定 parent .
另請參閱 listen ().
銷毀 QLocalServer 對象。若服務器正在監聽連接,它將自動關閉。
任何仍連接的客戶端 QLocalSockets 必須斷開連接,或重設父級在刪除服務器之前。
另請參閱 close ().
Stop listening for incoming connections. Existing connections are not effected, but any new connections will be refused.
另請參閱 isListening () 和 listen ().
返迴人類可讀消息適閤當前報告錯誤通過 serverError ()。若沒有閤適字符串可用,返迴空字符串。
另請參閱 serverError ().
返迴服務器正監聽的完整路徑。
注意:這特定於平颱
另請參閱 listen () 和 serverName ().
[虛擬]
bool
QLocalServer::
hasPendingConnections
() const
Returns true if the server has a pending connection; otherwise returns false.
另請參閱 nextPendingConnection () 和 setMaxPendingConnections ().
[virtual protected]
void
QLocalServer::
incomingConnection
(
quintptr
socketDescriptor
)
此虛函數被調用由 QLocalServer 當新連接可用時。 socketDescriptor 是已接受連接的本機套接字描述符。
基實現創建 QLocalSocket ,設置套接字描述符,然後存儲 QLocalSocket 在待決連接的內部列錶中。最後 newConnection () 發射。
重實現此函數以更改服務器行為,當連接可用時。
另請參閱 newConnection (), nextPendingConnection (),和 QLocalSocket::setSocketDescriptor ().
Returns true if the server is listening for incoming connections otherwise false.
告訴服務器去監聽傳入連接按 name 。若服務器目前在監聽,那麼它返迴 false。當成功時返迴 true,否則返迴 false。
name 可以是單個名稱且 QLocalServer 會確定正確的特定平颱路徑。 serverName () 將返迴被傳入 listen 的名稱。
通常,隻需傳遞像 foo 的名稱,但在 Unix 也可以是如 /tmp/foo 的路徑,而在 Windows 這可以是如 \\.\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.
另請參閱 serverName (), isListening (),和 close ().
返迴最大待決已接受連接數。默認為 30。
另請參閱 setMaxPendingConnections () 和 hasPendingConnections ().
[signal]
void
QLocalServer::
newConnection
()
此信號發射,每有當新連接可用時。
另請參閱 hasPendingConnections () 和 nextPendingConnection ().
[虛擬]
QLocalSocket
* QLocalServer::
nextPendingConnection
()
返迴下一待決連接作為連接的 QLocalSocket 對象。
套接字是作為服務器子級創建的,意味著會自動刪除它當 QLocalServer 對象被銷毀。明確刪除對象仍是好主意當這樣處理時,以避免浪費內存。
返迴 0 若在沒有待決連接時調用此函數。
另請參閱 hasPendingConnections (), newConnection (),和 incomingConnection ().
[static]
bool
QLocalServer::
removeServer
(const
QString
&
name
)
移除任何服務器實例可能導緻調用 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.
在 Windows,此函數什麼都不做;在 Unix,它移除套接字文件給定通過 name .
警告: 小心避免移除正運行實例的套接字。
該函數在 Qt 4.5 引入。
Returns the type of error that occurred last or NoError.
另請參閱 errorString ().
返迴服務器名稱若服務器正監聽連接;否則返迴 QString()
另請參閱 listen () 和 fullServerName ().
把最大待決接受連接數設為 numConnections . QLocalServer 將接受不超過 numConnections 傳入連接先於 nextPendingConnection () 被調用。
注意:即使 QLocalServer 將停止接受新連接,在達到其待決連接的最大數後,操作係統仍可能將它們保持在隊列中,這會導緻客戶端發齣已連接信號。
另請參閱 maxPendingConnections () 和 hasPendingConnections ().
等待最多 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 0, *timedOut will be set to true.
這是阻塞函數調用。在單綫程 GUI 應用程序中不建議使用它,由於整個應用程序將停止響應直到函數返迴。waitForNewConnection() 最有用,當沒有事件循環可用時。
非阻塞替代是連接到 newConnection () 信號。
若 msec 為 -1,此函數不會超時。
另請參閱 hasPendingConnections () 和 nextPendingConnection ().