QTcpServer 類提供基於 TCP 的服務器。 更多...
| 頭: | #include <QTcpServer> |
| qmake: | QT += network |
| 繼承: | QObject |
| 繼承者: |
注意: 此類的所有函數 可重入 .
| QTcpServer (QObject * parent = nullptr) | |
| virtual | ~QTcpServer () |
| void | close () |
| QString | errorString () const |
| virtual bool | hasPendingConnections () const |
| bool | isListening () const |
| bool | listen (const QHostAddress & address = QHostAddress::Any, quint16 port = 0) |
| int | maxPendingConnections () const |
| virtual QTcpSocket * | nextPendingConnection () |
| void | pauseAccepting () |
| QNetworkProxy | proxy () const |
| void | resumeAccepting () |
| QHostAddress | serverAddress () const |
| QAbstractSocket::SocketError | serverError () const |
| quint16 | serverPort () const |
| void | setMaxPendingConnections (int numConnections ) |
| void | setProxy (const QNetworkProxy & networkProxy ) |
| bool | setSocketDescriptor (qintptr socketDescriptor ) |
| qintptr | socketDescriptor () const |
| bool | waitForNewConnection (int msec = 0, bool * timedOut = nullptr) |
| void | acceptError (QAbstractSocket::SocketError socketError ) |
| void | newConnection () |
| void | addPendingConnection (QTcpSocket * socket ) |
| virtual void | incomingConnection (qintptr socketDescriptor ) |
此類使接受傳入 TCP 連接成為可能。可以指定端口或讓 QTcpServer 自動挑選一個。可以監聽特定地址或機器的所有地址。
調用 listen () 以讓服務器監聽傳入連接。 newConnection () 信號然後被發射,每次客戶端連接到服務器時。
調用 nextPendingConnection () 以接受待決連接作為已連接 QTcpSocket 。函數返迴的指針指嚮 QTcpSocket in QAbstractSocket::ConnectedState ,可以用於與客戶端進行通信。
若齣現錯誤, serverError () 返迴錯誤的類型,且 errorString () 可以被調用,以獲得發生什麼的人類可讀描述。
當監聽連接時,服務器正監聽的可用地址和端口為 serverAddress () 和 serverPort ().
調用 close () 使 QTcpServer 停止監聽傳入連接。
盡管 QTcpServer 主要是為與事件循環一起使用而設計的,但不使用事件循環是可能的。在此情況下,必須使用 waitForNewConnection () 阻塞直到連接可用或超時到期。
另請參閱 QTcpSocket , Fortune 服務器範例 , 綫程化 Fortune 服務器範例 , 迴環範例 ,和 Torrent 範例 .
構造 QTcpServer 對象。
parent 被傳遞給 QObject 構造函數。
另請參閱 listen () 和 setSocketDescriptor ().
[signal]
void
QTcpServer::
acceptError
(
QAbstractSocket::SocketError
socketError
)
此信號被發射當接受新連接導緻錯誤時。 socketError 參數描述齣現錯誤的類型。
該函數在 Qt 5.0 引入。
另請參閱 pauseAccepting () 和 resumeAccepting ().
[signal]
void
QTcpServer::
newConnection
()
此信號發射,每有當新連接可用時。
另請參閱 hasPendingConnections () 和 nextPendingConnection ().
[虛擬]
QTcpServer::
~QTcpServer
()
銷毀 QTcpServer 對象。若服務器正在監聽連接,套接字會自動關閉。
任何客戶端 QTcpSocket 仍連接的必須斷開連接,或重設父級在刪除服務器之前。
另請參閱 close ().
[protected]
void
QTcpServer::
addPendingConnection
(
QTcpSocket
*
socket
)
此函數被調用通過 QTcpServer::incomingConnection () 以添加 socket 到待決傳入連接列錶。
注意: 彆忘瞭調用此成員從重實現 incomingConnection () 若不想破壞待決連接機製。
該函數在 Qt 4.7 引入。
另請參閱 incomingConnection ().
關閉服務器。服務器將不再監聽傳入連接。
另請參閱 listen ().
返迴最近發生錯誤的人類可讀描述。
另請參閱 serverError ().
[虛擬]
bool
QTcpServer::
hasPendingConnections
() const
返迴
true
若服務器有待決連接;否則返迴
false
.
另請參閱 nextPendingConnection () 和 setMaxPendingConnections ().
[virtual protected]
void
QTcpServer::
incomingConnection
(
qintptr
socketDescriptor
)
此虛函數被調用由 QTcpServer 當新連接可用時。 socketDescriptor 自變量是已接受連接的本機套接字描述符。
基實現創建 QTcpSocket ,設置套接字描述符,然後存儲 QTcpSocket 在待決連接的內部列錶中。最後 newConnection () 發射。
重實現此函數以更改服務器行為,當連接可用時。
若此服務器正使用 QNetworkProxy 那麼 socketDescriptor 可能不可用於本機套接字函數,且隻應用於 QTcpSocket::setSocketDescriptor ().
注意: 若在此方法的重實現中創建另一套接字,需要將它添加到待決連接機製通過調用 addPendingConnection ().
注意: 若想要處理傳入連接作為新 QTcpSocket 對象在另一綫程中,必須傳遞 socketDescriptor 到其它綫程並創建 QTcpSocket 對象在那裏並使用其 setSocketDescriptor () 方法。
另請參閱 newConnection (), nextPendingConnection (),和 addPendingConnection ().
返迴
true
若服務器目前在監聽傳入連接;否則返迴
false
.
另請參閱 listen ().
告訴服務器去監聽傳入連接按地址 address 和端口 port 。若 port 為 0,自動選取端口。若 address is QHostAddress::Any ,服務器將監聽所有網絡接口。
返迴
true
當成功時;否則返迴
false
.
另請參閱 isListening ().
返迴最大待決已接受連接數。默認為 30。
另請參閱 setMaxPendingConnections () 和 hasPendingConnections ().
[虛擬]
QTcpSocket
*QTcpServer::
nextPendingConnection
()
返迴下一待決連接作為連接的 QTcpSocket 對象。
套接字是作為服務器子級創建的,意味著會自動刪除它當 QTcpServer 對象被銷毀。明確刪除對象仍是好主意當這樣處理時,以避免浪費內存。
nullptr
被返迴若在沒有待決連接時調用此函數。
注意: 返迴的 QTcpSocket 對象不可以用於另一綫程。若想要從另一綫程使用傳入連接,需要覆蓋 incomingConnection ().
另請參閱 hasPendingConnections ().
暫停接受新連接。排隊連接將保留在隊列中。
該函數在 Qt 5.0 引入。
另請參閱 resumeAccepting ().
返迴此套接字的網絡代理。默認情況下 QNetworkProxy::DefaultProxy 被使用。
該函數在 Qt 4.1 引入。
另請參閱 setProxy () 和 QNetworkProxy .
再繼續接受新連接。
該函數在 Qt 5.0 引入。
另請參閱 pauseAccepting ().
返迴服務器的地址,若服務器正在監聽連接;否則返迴 QHostAddress::Null .
另請參閱 serverPort () 和 listen ().
返迴最後發生錯誤的錯誤代碼。
另請參閱 errorString ().
返迴服務器端口,若服務器正監聽連接;否則返迴 0。
另請參閱 serverAddress () 和 listen ().
把最大待決接受連接數設為 numConnections . QTcpServer 將接受不超過 numConnections 傳入連接先於 nextPendingConnection () 被調用。默認情況下,限製為 30 個待決連接。
客戶端可能仍能連接,在服務器到達其最大待決連接數之後 (即: QTcpSocket 仍可以發射 connected() 信號)。 QTcpServer 將停止接受新連接,但操作係統可能仍將它們保留在隊列中。
另請參閱 maxPendingConnections () 和 hasPendingConnections ().
將此套接字的顯式網絡代理設為 networkProxy .
要禁用此套接字所用代理,使用 QNetworkProxy::NoProxy 代理類型:
server->setProxy(QNetworkProxy::NoProxy);
該函數在 Qt 4.1 引入。
另請參閱 proxy () 和 QNetworkProxy .
把此服務器在監聽傳入連接時應該使用的套接字描述符設為
socketDescriptor
。返迴
true
若套接字設置成功;否則返迴
false
.
假定套接字處於監聽狀態。
另請參閱 socketDescriptor () 和 isListening ().
返迴服務器用於監聽傳入指令的本機套接字描述符,或 -1 若服務器未監聽。
若服務器正使用 QNetworkProxy ,返迴的描述符可能不能用於本機套接字函數。
另請參閱 setSocketDescriptor () 和 isListening ().
等待最多
msec
毫秒或直到傳入連接可用。返迴
true
若連接可用;否則返迴
false
。若操作超時且
timedOut
不是
nullptr
, *
timedOut
將被設為 true。
這是阻塞函數調用。在單綫程 GUI 應用程序中不建議使用它,由於整個應用程序將停止響應直到函數返迴。waitForNewConnection() 最有用,當沒有事件循環可用時。
非阻塞替代是連接到 newConnection () 信號。
若 msec 為 -1,此函數不會超時。
另請參閱 hasPendingConnections () 和 nextPendingConnection ().