QTcpServer 類

The QTcpServer 類提供基於 TCP 的服務器。 更多...

頭: #include <QTcpServer>
繼承: QObject

注意: 此類的所有函數 可重入 .

公共函數

QTcpServer (QObject * parent = 0)
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 ()
QNetworkProxy proxy () const
QHostAddress serverAddress () const
QAbstractSocket::SocketError serverError () const
quint16 serverPort () const
void setMaxPendingConnections (int numConnections )
void setProxy (const QNetworkProxy & networkProxy )
bool setSocketDescriptor (int socketDescriptor )
int socketDescriptor () const
bool waitForNewConnection (int msec = 0, bool * timedOut = 0)

信號

void newConnection ()

保護函數

void addPendingConnection (QTcpSocket * socket )
virtual void incomingConnection (int socketDescriptor )

額外繼承成員

詳細描述

The QTcpServer 類提供基於 TCP 的服務器。

此類使接受傳入 TCP 連接,成為可能。 可以指定端口或讓 QTcpServer 自動挑選一個。可以監聽特定地址或所有機器地址。

調用 listen () 以讓服務器監聽傳入連接。 newConnection () 信號然後被發射,每次客戶端連接到服務器時。

調用 nextPendingConnection () 以接受待決連接作為已連接 QTcpSocket 。函數返迴的指針指嚮 QTcpSocket in QAbstractSocket::ConnectedState ,可以用於與客戶端進行通信。

若齣現錯誤, serverError () 返迴錯誤的類型,且 errorString () 可以被調用,以獲得發生什麼的人類可讀描述。

當監聽連接時,服務器正監聽的可用地址和端口為 serverAddress () 和 serverPort ().

調用 close () 使 QTcpServer 停止監聽傳入連接。

盡管 QTcpServer 主要是為與事件循環一起使用而設計的,但不使用事件循環是可能的。在此情況下,必須使用 waitForNewConnection () 阻塞直到連接可用或超時到期。

Symbian Platform Security Requirements

On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, it will lead to a panic.

Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.

另請參閱 QTcpSocket , Fortune 服務器範例 , 綫程化 Fortune 服務器範例 , 迴環範例 ,和 Torrent 範例 .

成員函數文檔編製

QTcpServer:: QTcpServer ( QObject * parent = 0)

構造 QTcpServer 對象。

parent 被傳遞給 QObject 構造函數。

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

[虛擬] QTcpServer:: ~QTcpServer ()

銷毀 QTcpServer 對象。若服務器正在監聽連接,套接字會自動關閉。

任何客戶端 QTcpSocket 仍連接的必須斷開連接,或重設父級在刪除服務器之前。

另請參閱 close ().

[protected] void QTcpServer:: addPendingConnection ( QTcpSocket * socket )

此函數被調用通過 QTcpServer::incomingConnection () 以添加 socket 到待決傳入連接列錶。

注意: 彆忘瞭調用此成員從重實現 incomingConnection () 若不想破壞待決連接機製。

該函數在 Qt 4.7 引入。

另請參閱 incomingConnection ().

void QTcpServer:: close ()

關閉服務器。服務器將不再監聽傳入連接。

另請參閱 listen ().

QString QTcpServer:: errorString () const

返迴最近發生錯誤的人類可讀描述。

另請參閱 serverError ().

[虛擬] bool QTcpServer:: hasPendingConnections () const

Returns true if the server has a pending connection; otherwise returns false.

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

[virtual protected] void QTcpServer:: incomingConnection ( int socketDescriptor )

此虛函數被調用由 QTcpServer 當新連接可用時。 socketDescriptor 自變量是已接受連接的本機套接字描述符。

基實現創建 QTcpSocket ,設置套接字描述符,然後存儲 QTcpSocket 在待決連接的內部列錶中。最後 newConnection () 發射。

重實現此函數以更改服務器行為,當連接可用時。

若此服務器正使用 QNetworkProxy 那麼 socketDescriptor 可能不可用於本機套接字函數,且隻應用於 QTcpSocket::setSocketDescriptor ().

注意: 若想要處理傳入連接作為新 QTcpSocket 對象在另一綫程中,必須傳遞 socketDescriptor 到其它綫程並創建 QTcpSocket 對象在那裏並使用其 setSocketDescriptor () 方法。

另請參閱 newConnection (), nextPendingConnection (),和 addPendingConnection ().

bool QTcpServer:: isListening () const

Returns true if the server is currently listening for incoming connections; otherwise returns false.

另請參閱 listen ().

bool QTcpServer:: listen (const QHostAddress & address = QHostAddress::Any, quint16 port = 0)

告訴服務器去監聽傳入連接按地址 address 和端口 port 。若 port 為 0,自動選取端口。若 address is QHostAddress::Any ,服務器將監聽所有網絡接口。

返迴 true 當成功時;否則返迴 false。

另請參閱 isListening ().

int QTcpServer:: maxPendingConnections () const

返迴最大待決已接受連接數。默認為 30。

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

[signal] void QTcpServer:: newConnection ()

此信號發射,每有當新連接可用時。

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

[虛擬] QTcpSocket * QTcpServer:: nextPendingConnection ()

返迴下一待決連接作為連接的 QTcpSocket 對象。

套接字是作為服務器子級創建的,意味著會自動刪除它當 QTcpServer 對象被銷毀。明確刪除對象仍是好主意當這樣處理時,以避免浪費內存。

返迴 0 若在沒有待決連接時調用此函數。

注意: 返迴的 QTcpSocket 對象不可以用於另一綫程。若想要從另一綫程使用傳入連接,需要覆蓋 incomingConnection ().

另請參閱 hasPendingConnections ().

QNetworkProxy QTcpServer:: proxy () const

返迴此套接字的網絡代理。默認情況下 QNetworkProxy::DefaultProxy 被使用。

該函數在 Qt 4.1 引入。

另請參閱 setProxy () 和 QNetworkProxy .

QHostAddress QTcpServer:: serverAddress () const

返迴服務器的地址,若服務器正在監聽連接;否則返迴 QHostAddress::Null .

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

QAbstractSocket::SocketError QTcpServer:: serverError () const

返迴最後發生錯誤的錯誤代碼。

另請參閱 errorString ().

quint16 QTcpServer:: serverPort () const

返迴服務器端口,若服務器正監聽連接;否則返迴 0。

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

void QTcpServer:: setMaxPendingConnections ( int numConnections )

把最大待決接受連接數設為 numConnections . QTcpServer 將接受不超過 numConnections 傳入連接先於 nextPendingConnection () 被調用。默認情況下,限製為 30 個待決連接。

客戶端可能仍能連接,在服務器到達其最大待決連接數之後 (即: QTcpSocket 仍可以發射 connected() 信號)。 QTcpServer 將停止接受新連接,但操作係統可能仍將它們保留在隊列中。

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

void QTcpServer:: setProxy (const QNetworkProxy & networkProxy )

將此套接字的顯式網絡代理設為 networkProxy .

要禁用此套接字所用代理,使用 QNetworkProxy::NoProxy 代理類型:

server->setProxy(QNetworkProxy::NoProxy);
					

該函數在 Qt 4.1 引入。

另請參閱 proxy () 和 QNetworkProxy .

bool QTcpServer:: setSocketDescriptor ( int socketDescriptor )

把此服務器在監聽傳入連接時應該使用的套接字描述符設為 socketDescriptor . Returns true if the socket is set successfully; otherwise returns false.

假定套接字處於監聽狀態。

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

int QTcpServer:: socketDescriptor () const

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

若服務器正使用 QNetworkProxy ,返迴的描述符可能不能用於本機套接字函數。

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

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

等待最多 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 將被設為 true。

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

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

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

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