QTcpServer 类

QTcpServer 类提供基于 TCP 的服务器。 更多...

头: #include <QTcpServer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Network)
target_link_libraries(mytarget PRIVATE Qt6::Network)
qmake: QT += network
继承: QObject
继承者: QSctpServer and QSslServer

注意: 此类的所有函数 可重入 .

公共函数

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 listenBacklogSize () const
int maxPendingConnections () const
virtual QTcpSocket * nextPendingConnection ()
void pauseAccepting ()
QNetworkProxy proxy () const
void resumeAccepting ()
QHostAddress serverAddress () const
QAbstractSocket::SocketError serverError () const
quint16 serverPort () const
void setListenBacklogSize (int size )
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 pendingConnectionAvailable ()

保护函数

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

详细描述

此类使接受传入 TCP 连接成为可能。可以指定端口或让 QTcpServer 自动挑选一个。可以监听特定地址或机器的所有地址。

调用 listen () 以让服务器监听传入连接。 newConnection () signal is then emitted each time a client connects to the server. When the client connection has been added to the pending connection queue using the addPendingConnection () function, the pendingConnectionAvailable () 信号发射。

调用 nextPendingConnection () 以接受待决连接作为已连接 QTcpSocket 。函数返回的指针指向 QTcpSocket in QAbstractSocket::ConnectedState ,可以用于与客户端进行通信。

若出现错误, serverError () 返回错误的类型,且 errorString () 可以被调用,以获得发生什么的人类可读描述。

当监听连接时,服务器正监听的可用地址和端口为 serverAddress () 和 serverPort ().

调用 close () 使 QTcpServer 停止监听传入连接。

尽管 QTcpServer 主要是为与事件循环一起使用而设计的,但不使用事件循环是可能的。在此情况下,必须使用 waitForNewConnection () 阻塞直到连接可用或超时到期。

另请参阅 QTcpSocket , Fortune 服务器 , 线程化 Fortune 服务器 ,和 Torrent 范例 .

成员函数文档编制

[explicit] QTcpServer:: QTcpServer ( QObject * parent = nullptr)

构造 QTcpServer 对象。

parent 被传递给 QObject 构造函数。

另请参阅 listen () 和 setSocketDescriptor ().

[虚拟] QTcpServer:: ~QTcpServer ()

销毁 QTcpServer 对象。若服务器正在监听连接,套接字会自动关闭。

任何客户端 QTcpSocket 仍连接的必须断开连接,或重设父级在删除服务器之前。

另请参阅 close ().

[signal] void QTcpServer:: acceptError ( QAbstractSocket::SocketError socketError )

此信号被发射当接受新连接导致错误时。 socketError 参数描述出现错误的类型。

另请参阅 pauseAccepting () 和 resumeAccepting ().

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

此函数被调用通过 QTcpServer::incomingConnection () 以添加 socket 到待决传入连接列表。

注意: 别忘了调用此成员从重实现 incomingConnection () if you do not want to break the Pending Connections mechanism. This function emits the pendingConnectionAvailable () signal after the socket has been added.

另请参阅 incomingConnection () 和 pendingConnectionAvailable ().

void QTcpServer:: close ()

关闭服务器。服务器将不再监听传入连接。

另请参阅 listen ().

QString QTcpServer:: errorString () const

返回最近发生错误的人类可读描述。

另请参阅 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 ().

bool QTcpServer:: isListening () const

返回 true 若服务器目前在监听传入连接;否则返回 false .

另请参阅 listen ().

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

告诉服务器去监听传入连接按地址 address 和端口 port 。若 port 为 0,自动选取端口。若 address is QHostAddress::Any ,服务器将监听所有网络接口。

返回 true 当成功时;否则返回 false .

另请参阅 isListening ().

[since 6.3] int QTcpServer:: listenBacklogSize () const

Returns the backlog queue size of to be accepted connections.

This function was introduced in Qt 6.3.

另请参阅 setListenBacklogSize ().

int QTcpServer:: maxPendingConnections () const

返回最大待决已接受连接数。默认为 30。

另请参阅 setMaxPendingConnections () 和 hasPendingConnections ().

[signal] void QTcpServer:: newConnection ()

This signal is emitted every time a new connection is available, regardless of whether it has been added to the pending connections queue or not.

另请参阅 hasPendingConnections () 和 nextPendingConnection ().

[虚拟] QTcpSocket *QTcpServer:: nextPendingConnection ()

返回下一待决连接作为连接的 QTcpSocket 对象。

套接字是作为服务器子级创建的,意味着会自动删除它当 QTcpServer 对象被销毁。明确删除对象仍是好主意当这样处理时,以避免浪费内存。

nullptr 被返回若在没有待决连接时调用此函数。

注意: 返回的 QTcpSocket 对象不可以用于另一线程。若想要从另一线程使用传入连接,需要覆盖 incomingConnection ().

另请参阅 hasPendingConnections ().

void QTcpServer:: pauseAccepting ()

暂停接受新连接。排队连接将保留在队列中。

另请参阅 resumeAccepting ().

[private signal, since 6.4] void QTcpServer:: pendingConnectionAvailable ()

This signal is emitted every time a new connection has been added to the pending connections queue.

注意: 这是私有信号。它可以用于信号连接,但不能由用户发射。

This function was introduced in Qt 6.4.

另请参阅 hasPendingConnections () 和 nextPendingConnection ().

QNetworkProxy QTcpServer:: proxy () const

返回此套接字的网络代理。默认情况下 QNetworkProxy::DefaultProxy 被使用。

另请参阅 setProxy () 和 QNetworkProxy .

void QTcpServer:: resumeAccepting ()

再继续接受新连接。

另请参阅 pauseAccepting ().

QHostAddress QTcpServer:: serverAddress () const

返回服务器的地址,若服务器正在监听连接;否则返回 QHostAddress::Null .

另请参阅 serverPort () 和 listen ().

QAbstractSocket::SocketError QTcpServer:: serverError () const

返回最后发生错误的错误代码。

另请参阅 errorString ().

quint16 QTcpServer:: serverPort () const

返回服务器端口,若服务器正监听连接;否则返回 0。

另请参阅 serverAddress () 和 listen ().

[since 6.3] void QTcpServer:: 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 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);
					

另请参阅 proxy () 和 QNetworkProxy .

bool QTcpServer:: setSocketDescriptor ( qintptr socketDescriptor )

把此服务器在监听传入连接时应该使用的套接字描述符设为 socketDescriptor 。返回 true 若套接字设置成功;否则返回 false .

假定套接字处于监听状态。

另请参阅 socketDescriptor () 和 isListening ().

qintptr QTcpServer:: socketDescriptor () const

返回服务器用于监听传入指令的本机套接字描述符,或 -1 若服务器未监听。

若服务器正使用 QNetworkProxy ,返回的描述符可能不能用于本机套接字函数。

另请参阅 setSocketDescriptor () 和 isListening ().

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

等待最多 msec 毫秒或直到传入连接可用。返回 true 若连接可用;否则返回 false 。若操作超时且 timedOut 不是 nullptr , * timedOut 将被设为 true。

这是阻塞函数调用。在单线程 GUI 应用程序中不建议使用它,由于整个应用程序将停止响应直到函数返回。waitForNewConnection() 最有用,当没有事件循环可用时。

非阻塞替代是连接到 newConnection () 信号。

若 msec 为 -1,此函数不会超时。

另请参阅 hasPendingConnections () 和 nextPendingConnection ().