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 () 阻塞直到连接可用或超时到期。
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 对象。
parent 被传递给 QObject 构造函数。
另请参阅 listen () 和 setSocketDescriptor ().
[虚拟]
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
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 ().
Returns true if the server is currently listening for incoming connections; otherwise returns false.
另请参阅 listen ().
告诉服务器去监听传入连接在地址 address 和端口 port 。若 port 为 0,自动选取端口。若 address is QHostAddress::Any ,服务器将监听所有网络接口。
返回 true 当成功时;否则返回 false。
另请参阅 isListening ().
返回最大待决已接受连接数。默认为 30。
另请参阅 setMaxPendingConnections () 和 hasPendingConnections ().
[signal]
void
QTcpServer::
newConnection
()
此信号被发射,每次有新连接可用时。
另请参阅 hasPendingConnections () 和 nextPendingConnection ().
[虚拟]
QTcpSocket
* QTcpServer::
nextPendingConnection
()
返回下一待决连接作为连接的 QTcpSocket 对象。
套接字是作为服务器子级创建的,意味着会自动删除它当 QTcpServer 对象被销毁。明确删除对象仍是好主意当这样处理时,以避免浪费内存。
返回 0 若在没有待决连接时调用此函数。
注意: 返回的 QTcpSocket 对象不可以用于另一线程。若想要从另一线程使用传入连接,需要覆盖 incomingConnection ().
另请参阅 hasPendingConnections ().
返回此套接字的网络代理。默认情况下 QNetworkProxy::DefaultProxy 被使用。
该函数在 Qt 4.1 引入。
另请参阅 setProxy () 和 QNetworkProxy .
返回服务器的地址,若服务器正在监听连接;否则返回 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 . Returns true if the socket is set successfully; otherwise returns false.
假定套接字处于监听状态。
另请参阅 socketDescriptor () 和 isListening ().
返回服务器用于监听传入指令的本机套接字描述符,或 -1 若服务器未监听。
若服务器正使用 QNetworkProxy ,返回的描述符可能不能用于本机套接字函数。
另请参阅 setSocketDescriptor () 和 isListening ().
等待最多 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 ().