QTcpServer 类

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

头: #include <QTcpServer>
qmake: QT += network
继承: QObject
继承者:

QSctpServer

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

公共函数

QTcpServer (QObject * parent = Q_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 = Q_NULLPTR)

信号

void acceptError (QAbstractSocket::SocketError socketError )
void newConnection ()

保护函数

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

额外继承成员

详细描述

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

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

调用 listen () 以让服务器监听传入连接。 newConnection () 信号然后被发射,每次客户端连接到服务器时。

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

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

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

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

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

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

成员函数文档编制

QTcpServer:: QTcpServer ( QObject * parent = Q_NULLPTR)

构造 QTcpServer 对象。

parent 被传递给 QObject 构造函数。

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

[virtual] QTcpServer:: ~QTcpServer ()

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

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

另请参阅 close ().

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

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

该函数在 Qt 5.0 引入。

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

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

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

注意: 别忘了调用此成员从重实现 incomingConnection () 若不想中断待决连接机制。

该函数在 Qt 4.7 引入。

另请参阅 incomingConnection ().

void QTcpServer:: close ()

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

另请参阅 listen ().

QString QTcpServer:: errorString () const

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

另请参阅 serverError ().

[virtual] 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 ().

int QTcpServer:: maxPendingConnections () const

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

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

[signal] void QTcpServer:: newConnection ()

此信号被发射,每次有新连接可用时。

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

[virtual] QTcpSocket *QTcpServer:: nextPendingConnection ()

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

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

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

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

另请参阅 hasPendingConnections ().

void QTcpServer:: pauseAccepting ()

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

该函数在 Qt 5.0 引入。

另请参阅 resumeAccepting ().

QNetworkProxy QTcpServer:: proxy () const

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

该函数在 Qt 4.1 引入。

另请参阅 setProxy () 和 QNetworkProxy .

void QTcpServer:: resumeAccepting ()

再继续接受新连接。

该函数在 Qt 5.0 引入。

另请参阅 pauseAccepting ().

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 ( qintptr socketDescriptor )

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

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

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

qintptr QTcpServer:: socketDescriptor () const

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

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

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

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

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

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

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

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

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