内容表

上一话题

QSslSocket

下一话题

QTcpSocket

QTcpServer

QTcpServer class provides a TCP-based server. 更多

Inheritance diagram of PySide2.QtNetwork.QTcpServer

概要

函数

信号

详细描述

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

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

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

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

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

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

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

class QTcpServer ( [ parent=None ] )
param parent

QObject

构造 QTcpServer 对象。

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

PySide2.QtNetwork.QTcpServer. acceptError ( socketError )
参数

socketError SocketError

PySide2.QtNetwork.QTcpServer. addPendingConnection ( socket )
参数

socket QTcpSocket

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

注意

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

PySide2.QtNetwork.QTcpServer. close ( )

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

另请参阅

listen()

PySide2.QtNetwork.QTcpServer. errorString ( )
返回类型

unicode

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

另请参阅

serverError()

PySide2.QtNetwork.QTcpServer. hasPendingConnections ( )
返回类型

bool

返回 true 若服务器有待决连接;否则返回 false .

PySide2.QtNetwork.QTcpServer. incomingConnection ( handle )
参数

handle qintptr

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

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

Reimplement this function to alter the server’s behavior when a connection is available.

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

注意

If another socket is created in the reimplementation of this method, it needs to be added to the Pending Connections mechanism by calling addPendingConnection() .

注意

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

PySide2.QtNetwork.QTcpServer. isListening ( )
返回类型

bool

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

另请参阅

listen()

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

bool

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

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

另请参阅

isListening()

PySide2.QtNetwork.QTcpServer. maxPendingConnections ( )
返回类型

int

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

PySide2.QtNetwork.QTcpServer. newConnection ( )
PySide2.QtNetwork.QTcpServer. nextPendingConnection ( )
返回类型

QTcpSocket

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

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

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

注意

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

PySide2.QtNetwork.QTcpServer. pauseAccepting ( )

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

另请参阅

resumeAccepting()

PySide2.QtNetwork.QTcpServer. proxy ( )
返回类型

QNetworkProxy

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

PySide2.QtNetwork.QTcpServer. resumeAccepting ( )

再继续接受新连接。

另请参阅

pauseAccepting()

PySide2.QtNetwork.QTcpServer. serverAddress ( )
返回类型

QHostAddress

Returns the server’s address if the server is listening for connections; otherwise returns Null .

PySide2.QtNetwork.QTcpServer. serverError ( )
返回类型

SocketError

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

另请参阅

errorString()

PySide2.QtNetwork.QTcpServer. serverPort ( )
返回类型

quint16

Returns the server’s port if the server is listening for connections; otherwise returns 0.

PySide2.QtNetwork.QTcpServer. setMaxPendingConnections ( numConnections )
参数

numConnections int

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

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

PySide2.QtNetwork.QTcpServer. setProxy ( networkProxy )
参数

networkProxy QNetworkProxy

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

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

server.setProxy(QNetworkProxy.NoProxy)
											
PySide2.QtNetwork.QTcpServer. setSocketDescriptor ( socketDescriptor )
参数

socketDescriptor qintptr

返回类型

bool

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

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

PySide2.QtNetwork.QTcpServer. socketDescriptor ( )
返回类型

qintptr

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

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

PySide2.QtNetwork.QTcpServer. waitForNewConnection ( msec )
参数

msec int

返回类型

(retval, timeOut)

等待最多 msec 毫秒或直到传入连接可用。返回 true 若连接可用;否则返回 false 。若操作超时且 timedOut 不是 None , *``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. is mostly useful when there is no event loop available.

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

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