QTcpServerclass provides a TCP-based server. 更多 …
def
addPendingConnection
(socket)
def
close
()
def
errorString
()
def
isListening
()
def
listen
([address=QHostAddress.Any[, port=0]])
def
maxPendingConnections
()
def
pauseAccepting
()
def
proxy
()
def
resumeAccepting
()
def
serverAddress
()
def
serverError
()
def
serverPort
()
def
setMaxPendingConnections
(numConnections)
def
setProxy
(networkProxy)
def
setSocketDescriptor
(socketDescriptor)
def
socketDescriptor
()
def
waitForNewConnection
(msec)
def
hasPendingConnections
()
def
incomingConnection
(handle)
def
nextPendingConnection
()
def
acceptError
(socketError)
def
newConnection
()
This class makes it possible to accept incoming TCP connections. You can specify the port or have
QTcpServerpick 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. ThenewConnection()signal is then emitted each time a client connects to the server.调用
nextPendingConnection()to accept the pending connection as a connectedQTcpSocket。函数返回的指针指向QTcpSocketinConnectedState,可以用于与客户端进行通信。若发生错误,
serverError()returns the type of error, anderrorString()can be called to get a human readable description of what happened.当监听连接时,服务器正监听的可用地址和端口为
serverAddress()andserverPort().调用
close()makesQTcpServerstop listening for incoming connections.尽管
QTcpServeris mostly designed for use with an event loop, it’s possible to use it without one. In that case, you must usewaitForNewConnection(), which blocks until either a connection is available or a timeout expires.
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.
errorString
(
)
¶
unicode
返回最近发生错误的人类可读描述。
另请参阅
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
.
另请参阅
PySide2.QtNetwork.QTcpServer.
listen
(
[
address=QHostAddress.Any
[
,
port=0
]
]
)
¶
address
–
QHostAddress
port
–
quint16
bool
告诉服务器去监听传入连接在地址
address
和端口
port
。若
port
为 0,自动选取端口。若
address
is
Any
,服务器将监听所有网络接口。
返回
true
当成功时;否则返回
false
.
另请参阅
PySide2.QtNetwork.QTcpServer.
maxPendingConnections
(
)
¶
int
返回最大待决已接受连接数。默认为 30。
PySide2.QtNetwork.QTcpServer.
newConnection
(
)
¶
PySide2.QtNetwork.QTcpServer.
nextPendingConnection
(
)
¶
返回下一待决连接作为已连接
QTcpSocket
对象。
套接字是作为服务器子级创建的,意味着它会被自动删除当
QTcpServer
对象被销毁。当采用完成后明确删除对象仍是好主意,以避免浪费内存。
None
被返回若在没有待决连接时调用此函数。
注意
返回的
QTcpSocket
对象不可以用于另一线程。若希望从另一线程使用传入连接,需要覆盖
incomingConnection()
.
PySide2.QtNetwork.QTcpServer.
pauseAccepting
(
)
¶
暂停接受新连接。排队连接将保留在队列中。
另请参阅
PySide2.QtNetwork.QTcpServer.
proxy
(
)
¶
返回此套接字的网络代理。默认情况下
DefaultProxy
被使用。
另请参阅
PySide2.QtNetwork.QTcpServer.
resumeAccepting
(
)
¶
再继续接受新连接。
另请参阅
PySide2.QtNetwork.QTcpServer.
serverAddress
(
)
¶
Returns the server’s address if the server is listening for connections; otherwise returns
Null
.
另请参阅
PySide2.QtNetwork.QTcpServer.
serverError
(
)
¶
SocketError
返回最后发生错误的错误代码。
另请参阅
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,此函数不会超时。