QTcpServer

概要

函数

信号

详细描述

PySide.QtNetwork.QTcpServer class provides a TCP-based server.

This class makes it possible to accept incoming TCP connections. You can specify the port or have PySide.QtNetwork.QTcpServer pick one automatically. You can listen on a specific address or on all the machine's addresses.

调用 PySide.QtNetwork.QTcpServer.listen() to have the server listen for incoming connections. The PySide.QtNetwork.QTcpServer.newConnection() signal is then emitted each time a client connects to the server.

调用 PySide.QtNetwork.QTcpServer.nextPendingConnection() to accept the pending connection as a connected PySide.QtNetwork.QTcpSocket 。函数返回的指针指向 PySide.QtNetwork.QTcpSocket in QAbstractSocket.ConnectedState ,可以用于与客户端进行通信。

若发生错误, PySide.QtNetwork.QTcpServer.serverError() returns the type of error, and PySide.QtNetwork.QTcpServer.errorString() can be called to get a human readable description of what happened.

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

调用 PySide.QtNetwork.QTcpServer.close() makes PySide.QtNetwork.QTcpServer stop listening for incoming connections.

尽管 PySide.QtNetwork.QTcpServer is mostly designed for use with an event loop, it's possible to use it without one. In that case, you must use PySide.QtNetwork.QTcpServer.waitForNewConnection() , which blocks until either a connection is available or a timeout expires.

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.

另请参阅

PySide.QtNetwork.QTcpSocket Fortune 服务器范例 线程化 Fortune 服务器范例 回环范例 Torrent Example

class PySide.QtNetwork. QTcpServer ( [ parent=None ] )
参数: parent PySide.QtCore.QObject

构造 PySide.QtNetwork.QTcpServer 对象。

parent 会被传递给 PySide.QtCore.QObject 构造函数。

PySide.QtNetwork.QTcpServer. addPendingConnection ( socket )
参数: socket PySide.QtNetwork.QTcpSocket

此函数被调用由 QTcpServer.incomingConnection() to add the socket to the list of pending incoming connections.

注意

Don't forget to call this member from reimplemented PySide.QtNetwork.QTcpServer.incomingConnection() if you do not want to break the Pending Connections mechanism.

PySide.QtNetwork.QTcpServer. close ( )

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

PySide.QtNetwork.QTcpServer. errorString ( )
返回类型: unicode

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

PySide.QtNetwork.QTcpServer. hasPendingConnections ( )
返回类型: PySide.QtCore.bool

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

PySide.QtNetwork.QTcpServer. incomingConnection ( handle )
参数: handle PySide.QtCore.int

此虚拟函数被调用由 PySide.QtNetwork.QTcpServer 当新连接可用时。 socketDescriptor 自变量是已接受连接的本机套接字描述符。

基实现创建 PySide.QtNetwork.QTcpSocket ,设置套接字描述符,然后存储 PySide.QtNetwork.QTcpSocket 在待决连接的内部列表中。最后 PySide.QtNetwork.QTcpServer.newConnection() is emitted.

重实现此函数以更改服务器行为,当连接可用时。

若此服务器正在使用 PySide.QtNetwork.QNetworkProxy 那么 socketDescriptor may not be usable with native socket functions, and should only be used with QTcpSocket.setSocketDescriptor() .

注意

If you want to handle an incoming connection as a new PySide.QtNetwork.QTcpSocket object in another thread you have to pass the socketDescriptor to the other thread and create the PySide.QtNetwork.QTcpSocket object there and use its PySide.QtNetwork.QTcpServer.setSocketDescriptor() 方法。

PySide.QtNetwork.QTcpServer. isListening ( )
返回类型: PySide.QtCore.bool

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

PySide.QtNetwork.QTcpServer. listen ( [ address=QHostAddress.Any [ , port=0 ] ] )
参数:
返回类型:

PySide.QtCore.bool

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

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

PySide.QtNetwork.QTcpServer. maxPendingConnections ( )
返回类型: PySide.QtCore.int

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

PySide.QtNetwork.QTcpServer. newConnection ( )
PySide.QtNetwork.QTcpServer. nextPendingConnection ( )
返回类型: PySide.QtNetwork.QTcpSocket

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

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

0 is returned if this function is called when there are no pending connections.

注意

返回的 PySide.QtNetwork.QTcpSocket 对象不可以用于另一线程。若希望从另一线程使用传入连接,需要覆盖 PySide.QtNetwork.QTcpServer.incomingConnection() .

PySide.QtNetwork.QTcpServer. proxy ( )
返回类型: PySide.QtNetwork.QNetworkProxy

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

PySide.QtNetwork.QTcpServer. serverAddress ( )
返回类型: PySide.QtNetwork.QHostAddress

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

PySide.QtNetwork.QTcpServer. serverError ( )
返回类型: PySide.QtNetwork.QAbstractSocket.SocketError

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

PySide.QtNetwork.QTcpServer. serverPort ( )
返回类型: PySide.QtCore.quint16

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

PySide.QtNetwork.QTcpServer. setMaxPendingConnections ( numConnections )
参数: numConnections PySide.QtCore.int

把最大待决接受连接数设为 numConnections . PySide.QtNetwork.QTcpServer 将接受不超过 numConnections 传入连接先于 PySide.QtNetwork.QTcpServer.nextPendingConnection() is called. By default, the limit is 30 pending connections.

客户端可能仍能连接,在服务器到达其最大待决连接数之后 (即: PySide.QtNetwork.QTcpSocket 仍可以发射 connected() 信号)。 PySide.QtNetwork.QTcpServer 将停止接受新连接,但操作系统可能仍将它们保留在队列中。

PySide.QtNetwork.QTcpServer. setProxy ( networkProxy )
参数: networkProxy PySide.QtNetwork.QNetworkProxy

将此套接字的显式网络代理设为 networkProxy .

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

server.setProxy(QNetworkProxy.NoProxy)
												
PySide.QtNetwork.QTcpServer. setSocketDescriptor ( socketDescriptor )
参数: socketDescriptor PySide.QtCore.int
返回类型: PySide.QtCore.bool

把此服务器在监听传入连接时应该使用的套接字描述符设为 socketDescriptor . Returns true if the socket is set successfully; otherwise returns false.

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

PySide.QtNetwork.QTcpServer. socketDescriptor ( )
返回类型: PySide.QtCore.int

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

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

PySide.QtNetwork.QTcpServer. waitForNewConnection ( msec )
参数: msec PySide.QtCore.int
返回类型: (retval, timeOut)

等待最多 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`` will be set to true.

This is a blocking function call. Its use is disadvised in a single-threaded GUI application, since the whole application will stop responding until the function returns. PySide.QtNetwork.QTcpServer.waitForNewConnection() is mostly useful when there is no event loop available.

非阻塞替代是连接到 PySide.QtNetwork.QTcpServer.newConnection() 信号。

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