QAbstractSocketclass provides the base functionality common to all socket types. 更多 …
继承者: QSslSocket , QTcpSocket , QUdpSocket
def
abort
()
def
bind
([port=0[, mode=QAbstractSocket.DefaultForPlatform]])
def
bind
(address[, port=0[, mode=QAbstractSocket.DefaultForPlatform]])
def
error
()
def
flush
()
def
isValid
()
def
localAddress
()
def
localPort
()
def
pauseMode
()
def
peerAddress
()
def
peerName
()
def
peerPort
()
def
protocolTag
()
def
proxy
()
def
readBufferSize
()
def
setLocalAddress
(address)
def
setLocalPort
(port)
def
setPauseMode
(pauseMode)
def
setPeerAddress
(address)
def
setPeerName
(name)
def
setPeerPort
(port)
def
setProtocolTag
(tag)
def
setProxy
(networkProxy)
def
setSocketError
(socketError)
def
setSocketState
(state)
def
socketType
()
def
state
()
def
connectToHost
(address, port[, mode=QIODevice.ReadWrite])
def
connectToHost
(hostName, port[, mode=QIODevice.ReadWrite[, protocol=AnyIPProtocol]])
def
disconnectFromHost
()
def
resume
()
def
setReadBufferSize
(size)
def
setSocketDescriptor
(socketDescriptor[, state=ConnectedState[, openMode=QIODevice.ReadWrite]])
def
setSocketOption
(option, value)
def
socketDescriptor
()
def
socketOption
(option)
def
waitForConnected
([msecs=30000])
def
waitForDisconnected
([msecs=30000])
def
connected
()
def
disconnected
()
def
error
(arg__1)
def
errorOccurred
(arg__1)
def
hostFound
()
def
proxyAuthenticationRequired
(proxy, authenticator)
def
stateChanged
(arg__1)
QAbstractSocketis the base class forQTcpSocketandQUdpSocket及包含这 2 个类的所有常用功能。若需要套接字,有 2 选项:
实例化
QTcpSocketorQUdpSocket.Create a native socket descriptor, instantiate
QAbstractSocket, and callsetSocketDescriptor()to wrap the native socket.TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol. UDP (User Datagram Protocol) is an unreliable, datagram-oriented, connectionless protocol. In practice, this means that TCP is better suited for continuous transmission of data, whereas the more lightweight UDP can be used when reliability isn’t important.
QAbstractSocket‘s API unifies most of the differences between the two protocols. For example, although UDP is connectionless,connectToHost()establishes a virtual connection for UDP sockets, enabling you to useQAbstractSocketin more or less the same way regardless of the underlying protocol. Internally,QAbstractSocketremembers the address and port passed toconnectToHost(), and functions likeread()andwrite()use these values.At any time,
QAbstractSockethas a state (returned bystate()). The initial state isUnconnectedState. After callingconnectToHost(), the socket first entersHostLookupState. If the host is found,QAbstractSocket进入ConnectingStateand emits thehostFound()signal. When the connection has been established, it entersConnectedState并发射connected(). If an error occurs at any stage,errorOccurred()is emitted. Whenever the state changes,stateChanged()is emitted. For convenience,isValid()返回trueif the socket is ready for reading and writing, but note that the socket’s state must beConnectedState在读写可以发生之前。Read or write data by calling
read()orwrite(), or use the convenience functionsreadLine()andreadAll().QAbstractSocketalso inheritsgetChar(),putChar(),和ungetChar()fromQIODevice, which work on single bytes. ThebytesWritten()signal is emitted when data has been written to the socket. Note that Qt does not limit the write buffer size. You can monitor its size by listening to this signal.
readyRead()signal is emitted every time a new chunk of data has arrived.bytesAvailable()then returns the number of bytes that are available for reading. Typically, you would connect thereadyRead()signal to a slot and read all available data there. If you don’t read all the data at once, the remaining data will still be available later, and any new incoming data will be appended toQAbstractSocket‘s internal read buffer. To limit the size of the read buffer, callsetReadBufferSize().要关闭套接字,调用
disconnectFromHost().QAbstractSocket进入ClosingState. After all pending data has been written to the socket,QAbstractSocketactually closes the socket, entersUnconnectedState,和发射disconnected(). If you want to abort a connection immediately, discarding all pending data, callabort()instead. If the remote host closes the connection,QAbstractSocket将发射errorOccurred(RemoteHostClosedError), during which the socket state will still beConnectedState, and then thedisconnected()signal will be emitted.The port and address of the connected peer is fetched by calling
peerPort()andpeerAddress().peerName()returns the host name of the peer, as passed toconnectToHost().localPort()andlocalAddress()return the port and address of the local socket.
QAbstractSocketprovides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking sockets:
waitForConnected()blocks until a connection has been established.
waitForReadyRead()blocks until new data is available for reading.
waitForBytesWritten()blocks until one payload of data has been written to the socket.
waitForDisconnected()blocks until the connection has closed.举例说明:
numRead = 0 numReadTotal = 0 while(True): buffer = socket.read(50) # do whatever with array numReadTotal += buffer.size() if (buffer.size() == 0 && !socket.waitForReadyRead()): break若
waitForReadyRead()返回false, the connection has been closed or an error has occurred.Programming with a blocking socket is radically different from programming with a non-blocking socket. A blocking socket doesn’t require an event loop and typically leads to simpler code. However, in a GUI application, blocking sockets should only be used in non-GUI threads, to avoid freezing the user interface. See the fortuneclient and blockingfortuneclient examples for an overview of both approaches.
注意
We discourage the use of the blocking functions together with signals. One of the two possibilities should be used.
QAbstractSocketcan be used withQTextStreamandQDataStream‘s stream operators (operator<<() and operator>>()). There is one issue to be aware of, though: You must make sure that enough data is available before attempting to read it using operator>>().
QAbstractSocket
(
socketType
,
parent
)
¶
- param parent
QObject- param socketType
SocketType
Creates a new abstract socket of type
socketType
。
parent
自变量会被传递给
QObject
‘s constructor.
PySide2.QtNetwork.QAbstractSocket.
SocketType
¶
此枚举描述传输层协议。
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.TcpSocket |
TCP |
|
QAbstractSocket.UdpSocket |
UDP |
|
QAbstractSocket.SctpSocket |
SCTP |
|
QAbstractSocket.UnknownSocketType |
除了 TCP、UDP 及 SCTP |
另请参阅
PySide2.QtNetwork.QAbstractSocket.
NetworkLayerProtocol
¶
This enum describes the network layer protocol values used in Qt.
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.IPv4Protocol |
IPv4 |
|
QAbstractSocket.IPv6Protocol |
IPv6 |
|
QAbstractSocket.AnyIPProtocol |
IPv4 或 IPv6 |
|
QAbstractSocket.UnknownNetworkLayerProtocol |
除了 IPv4 和 IPv6 |
另请参阅
PySide2.QtNetwork.QAbstractSocket.
SocketError
¶
此枚举描述可以发生的套接字错误。
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.ConnectionRefusedError |
连接被对等方拒绝 (或超时)。 |
|
QAbstractSocket.RemoteHostClosedError |
The remote host closed the connection. Note that the client socket (i.e., this socket) will be closed after the remote close notification has been sent. |
|
QAbstractSocket.HostNotFoundError |
找不到主机地址。 |
|
QAbstractSocket.SocketAccessError |
套接字操作失败,因为应用程序缺乏所需特权。 |
|
QAbstractSocket.SocketResourceError |
本地系统资源不足 (如:太多套接字)。 |
|
QAbstractSocket.SocketTimeoutError |
套接字操作超时。 |
|
QAbstractSocket.DatagramTooLargeError |
The datagram was larger than the operating system’s limit (which can be as low as 8192 bytes). |
|
QAbstractSocket.NetworkError |
An error occurred with the network (e.g., the network cable was accidentally plugged out). |
|
QAbstractSocket.AddressInUseError |
The address specified to
|
|
QAbstractSocket.SocketAddressNotAvailableError |
The address specified to
|
|
QAbstractSocket.UnsupportedSocketOperationError |
The requested socket operation is not supported by the local operating system (e.g., lack of IPv6 support). |
|
QAbstractSocket.ProxyAuthenticationRequiredError |
The socket is using a proxy, and the proxy requires authentication. |
|
QAbstractSocket.SslHandshakeFailedError |
The SSL/TLS handshake failed, so the connection was closed (only used in
|
|
QAbstractSocket.UnfinishedSocketOperationError |
Used by QAbstractSocketEngine only, The last operation attempted has not finished yet (still in progress in the background). |
|
QAbstractSocket.ProxyConnectionRefusedError |
Could not contact the proxy server because the connection to that server was denied |
|
QAbstractSocket.ProxyConnectionClosedError |
The connection to the proxy server was closed unexpectedly (before the connection to the final peer was established) |
|
QAbstractSocket.ProxyConnectionTimeoutError |
The connection to the proxy server timed out or the proxy server stopped responding in the authentication phase. |
|
QAbstractSocket.ProxyNotFoundError |
The proxy address set with
|
|
QAbstractSocket.ProxyProtocolError |
The connection negotiation with the proxy server failed, because the response from the proxy server could not be understood. |
|
QAbstractSocket.OperationError |
试图操作当套接字处于不准许状态时。 |
|
QAbstractSocket.SslInternalError |
The SSL library being used reported an internal error. This is probably the result of a bad installation or misconfiguration of the library. |
|
QAbstractSocket.SslInvalidUserDataError |
Invalid data (certificate, key, cypher, etc.) was provided and its use resulted in an error in the SSL library. |
|
QAbstractSocket.TemporaryError |
A temporary error occurred (e.g., operation would block and socket is non-blocking). |
|
QAbstractSocket.UnknownSocketError |
发生无法识别的错误。 |
另请参阅
PySide2.QtNetwork.QAbstractSocket.
SocketState
¶
此枚举描述套接字可以处于的不同状态。
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.UnconnectedState |
套接字未连接。 |
|
QAbstractSocket.HostLookupState |
套接字正在履行主机名查找。 |
|
QAbstractSocket.ConnectingState |
套接字开始建立连接。 |
|
QAbstractSocket.ConnectedState |
已建立连接。 |
|
QAbstractSocket.BoundState |
套接字被绑定到地址和端口。 |
|
QAbstractSocket.ClosingState |
套接字即将关闭 (数据可能仍在等待被写入)。 |
|
QAbstractSocket.ListeningState |
仅供内部使用。 |
另请参阅
PySide2.QtNetwork.QAbstractSocket.
SocketOption
¶
This enum represents the options that can be set on a socket. If desired, they can be set after having received the
connected()
signal from the socket or after having received a new socket from a
QTcpServer
.
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.LowDelayOption |
Try to optimize the socket for low latency. For a
|
|
QAbstractSocket.KeepAliveOption |
Set this to 1 to enable the SO_KEEPALIVE socket option |
|
QAbstractSocket.MulticastTtlOption |
Set this to an integer value to set IP_MULTICAST_TTL (TTL for multicast datagrams) socket option. |
|
QAbstractSocket.MulticastLoopbackOption |
Set this to 1 to enable the IP_MULTICAST_LOOP (multicast loopback) socket option. |
|
QAbstractSocket.TypeOfServiceOption |
This option is not supported on Windows. This maps to the IP_TOS socket option. For possible values, see table below. |
|
QAbstractSocket.SendBufferSizeSocketOption |
Sets the socket send buffer size in bytes at the OS level. This maps to the SO_SNDBUF socket option. This option does not affect the
|
|
QAbstractSocket.ReceiveBufferSizeSocketOption |
Sets the socket receive buffer size in bytes at the OS level. This maps to the SO_RCVBUF socket option. This option does not affect the
|
|
QAbstractSocket.PathMtuSocketOption |
Retrieves the Path Maximum Transmission Unit (PMTU) value currently known by the IP stack, if any. Some IP stacks also allow setting the MTU for transmission. This enum value was introduced in Qt 5.11. |
Possible values for ** are:
|
值 |
描述 |
|
224 |
Network control |
|
192 |
Internetwork control |
|
160 |
CRITIC/ECP |
|
128 |
Flash override |
|
96 |
Flash |
|
64 |
Immediate |
|
32 |
Priority |
|
0 |
Routine |
4.6 版新增。
PySide2.QtNetwork.QAbstractSocket.
BindFlag
¶
This enum describes the different flags you can pass to modify the behavior of
bind()
.
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.ShareAddress |
Allow other services to bind to the same address and port. This is useful when multiple processes share the load of a single service by listening to the same address and port (e.g., a web server with several pre-forked listeners can greatly improve response time). However, because any service is allowed to rebind, this option is subject to certain security considerations. Note that by combining this option with , you will also allow your service to rebind an existing shared address. On Unix, this is equivalent to the SO_REUSEADDR socket option. On Windows, this is the default behavior, so this option is ignored. |
|
QAbstractSocket.DontShareAddress |
Bind the address and port exclusively, so that no other services are allowed to rebind. By passing this option to
|
|
QAbstractSocket.ReuseAddressHint |
Provides a hint to
|
|
QAbstractSocket.DefaultForPlatform |
The default option for the current platform. On Unix and macOS, this is equivalent to ( + ), and on Windows, it is equivalent to . |
PySide2.QtNetwork.QAbstractSocket.
PauseMode
¶
This enum describes the behavior of when the socket should hold back with continuing data transfer. The only notification currently supported is
sslErrors()
.
|
常量 |
描述 |
|---|---|
|
QAbstractSocket.PauseNever |
Do not pause data transfer on the socket. This is the default and matches the behavior of Qt 4. |
|
QAbstractSocket.PauseOnSslErrors |
Pause data transfer on the socket upon receiving an SSL error notification. I.E.
|
PySide2.QtNetwork.QAbstractSocket.
abort
(
)
¶
中止当前连接并重置套接字。不像
disconnectFromHost()
, this function immediately closes the socket, discarding any pending data in the write buffer.
另请参阅
disconnectFromHost()
close()
PySide2.QtNetwork.QAbstractSocket.
bind
(
[
port=0
[
,
mode=QAbstractSocket.DefaultForPlatform
]
]
)
¶
port
–
quint16
mode
–
BindMode
bool
这是重载函数。
Binds to
QHostAddress
:Any on port
port
,使用
BindMode
mode
.
默认情况下,绑定套接字使用
DefaultForPlatform
BindMode
。若端口未指定,就选取随机端口。
PySide2.QtNetwork.QAbstractSocket.
bind
(
address
[
,
port=0
[
,
mode=QAbstractSocket.DefaultForPlatform
]
]
)
¶
address
–
QHostAddress
port
–
quint16
mode
–
BindMode
bool
Binds to
address
在端口
port
,使用
BindMode
mode
.
For UDP sockets, after binding, the signal
readyRead()
is emitted whenever a UDP datagram arrives on the specified address and port. Thus, this function is useful to write UDP servers.
For TCP sockets, this function may be used to specify which interface to use for an outgoing connection, which is useful in case of multiple network interfaces.
默认情况下,绑定套接字使用
DefaultForPlatform
BindMode
。若端口未指定,就选取随机端口。
On success, the function returns
true
and the socket enters
BoundState
;否则它返回
false
.
PySide2.QtNetwork.QAbstractSocket.
connectToHost
(
address
,
port
[
,
mode=QIODevice.ReadWrite
]
)
¶
address
–
QHostAddress
port
–
quint16
mode
–
OpenMode
这是重载函数。
试图连接到
address
在端口
port
.
PySide2.QtNetwork.QAbstractSocket.
connectToHost
(
hostName
,
port
[
,
mode=QIODevice.ReadWrite
[
,
protocol=AnyIPProtocol
]
]
)
¶
hostName – unicode
port
–
quint16
mode
–
OpenMode
protocol
–
NetworkLayerProtocol
试图连接到
hostName
on the given
port
。
protocol
parameter can be used to specify which network protocol to use (eg. IPv4 or IPv6).
套接字被打开采用给定
openMode
并首先进入
HostLookupState
, then performs a host name lookup of
hostName
. If the lookup succeeds,
hostFound()
is emitted and
QAbstractSocket
进入
ConnectingState
. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established,
QAbstractSocket
进入
ConnectedState
并发射
connected()
.
At any point, the socket can emit
errorOccurred()
to signal that an error occurred.
hostName
may be an IP address in string form (e.g., “43.195.83.32”), or it may be a host name (e.g., “example.com”).
QAbstractSocket
will do a lookup only if required.
port
is in native byte order.
PySide2.QtNetwork.QAbstractSocket.
connected
(
)
¶
PySide2.QtNetwork.QAbstractSocket.
disconnectFromHost
(
)
¶
试图关闭套接字。若有等待写入的待决数据,
QAbstractSocket
将进入
ClosingState
并等待直到所有数据被写入。最终,它将进入
UnconnectedState
and emit the
disconnected()
信号。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
disconnected
(
)
¶
PySide2.QtNetwork.QAbstractSocket.
error
(
arg__1
)
¶
arg__1
–
SocketError
注意
此函数被弃用。
PySide2.QtNetwork.QAbstractSocket.
errorOccurred
(
arg__1
)
¶
arg__1
–
SocketError
PySide2.QtNetwork.QAbstractSocket.
flush
(
)
¶
bool
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns
true
;否则 false 被返回。
调用此函数若需要
QAbstractSocket
立即开始发送缓冲数据。成功写入的字节数从属操作系统。在大多数情况下,不需调用此函数,因为
QAbstractSocket
将开始自动发送数据,一旦控制回到事件循环。若缺乏事件循环,调用
waitForBytesWritten()
代替。
另请参阅
write()
waitForBytesWritten()
PySide2.QtNetwork.QAbstractSocket.
hostFound
(
)
¶
PySide2.QtNetwork.QAbstractSocket.
isValid
(
)
¶
bool
返回
true
若套接字是有效的且准备使用;否则返回
false
.
注意
The socket’s state must be
ConnectedState
在读写可以发生之前。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
localAddress
(
)
¶
Returns the host address of the local socket if available; otherwise returns
Null
.
This is normally the main IP address of the host, but can be
LocalHost
(127.0.0.1) for connections to the local host.
PySide2.QtNetwork.QAbstractSocket.
localPort
(
)
¶
quint16
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0.
PySide2.QtNetwork.QAbstractSocket.
pauseMode
(
)
¶
PauseModes
返回此套接字的暂停模式。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
peerAddress
(
)
¶
Returns the address of the connected peer if the socket is in
ConnectedState
;否则返回
Null
.
PySide2.QtNetwork.QAbstractSocket.
peerName
(
)
¶
unicode
返回对等方名称,指定通过
connectToHost()
, or an empty
QString
if
connectToHost()
has not been called.
PySide2.QtNetwork.QAbstractSocket.
peerPort
(
)
¶
quint16
Returns the port of the connected peer if the socket is in
ConnectedState
; otherwise returns 0.
PySide2.QtNetwork.QAbstractSocket.
protocolTag
(
)
¶
unicode
Returns the protocol tag for this socket. If the protocol tag is set then this is passed to
QNetworkProxyQuery
when this is created internally to indicate the protocol tag to be used.
PySide2.QtNetwork.QAbstractSocket.
proxy
(
)
¶
返回此套接字的网络代理。默认情况下
DefaultProxy
is used, which means this socket will query the default proxy settings for the application.
PySide2.QtNetwork.QAbstractSocket.
proxyAuthenticationRequired
(
proxy
,
authenticator
)
¶
proxy
–
QNetworkProxy
authenticator
–
QAuthenticator
PySide2.QtNetwork.QAbstractSocket.
readBufferSize
(
)
¶
qint64
返回内部读取缓冲尺寸。这限制客户端可以接收的数据量先于调用
read()
or
readAll()
.
A read buffer size of 0 (the default) means that the buffer has no size limit, ensuring that no data is lost.
另请参阅
setReadBufferSize()
read()
PySide2.QtNetwork.QAbstractSocket.
resume
(
)
¶
继续在套接字中传输数据。才应该使用此方法仅在套接字被设为当通知时暂停且有收到通知之后。目前唯一支持的通知是
sslErrors()
. Calling this method if the socket is not paused results in undefined behavior.
PySide2.QtNetwork.QAbstractSocket.
setLocalAddress
(
address
)
¶
address
–
QHostAddress
Sets the address on the local side of a connection to
address
.
You can call this function in a subclass of
QAbstractSocket
to change the return value of the
localAddress()
function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
Note that this function does not bind the local address of the socket prior to a connection (e.g.,
bind()
).
PySide2.QtNetwork.QAbstractSocket.
setLocalPort
(
port
)
¶
port
–
quint16
Sets the port on the local side of a connection to
port
.
You can call this function in a subclass of
QAbstractSocket
to change the return value of the
localPort()
function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
Note that this function does not bind the local port of the socket prior to a connection (e.g.,
bind()
).
PySide2.QtNetwork.QAbstractSocket.
setPauseMode
(
pauseMode
)
¶
pauseMode
–
PauseModes
控制当收到通知时是否暂停。
pauseMode
parameter specifies the conditions in which the socket should be paused. The only notification currently supported is
sslErrors()
。若设为
PauseOnSslErrors
, data transfer on the socket will be paused and needs to be enabled explicitly again by calling
resume()
. By default this option is set to
PauseNever
. This option must be called before connecting to the server, otherwise it will result in undefined behavior.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
setPeerAddress
(
address
)
¶
address
–
QHostAddress
Sets the address of the remote side of the connection to
address
.
You can call this function in a subclass of
QAbstractSocket
to change the return value of the
peerAddress()
function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
PySide2.QtNetwork.QAbstractSocket.
setPeerName
(
name
)
¶
name – unicode
Sets the host name of the remote peer to
name
.
You can call this function in a subclass of
QAbstractSocket
to change the return value of the
peerName()
function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
setPeerPort
(
port
)
¶
port
–
quint16
Sets the port of the remote side of the connection to
port
.
You can call this function in a subclass of
QAbstractSocket
to change the return value of the
peerPort()
function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
PySide2.QtNetwork.QAbstractSocket.
setProtocolTag
(
tag
)
¶
tag – unicode
把此套接字的协议 Tag 设为
tag
.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
setProxy
(
networkProxy
)
¶
networkProxy
–
QNetworkProxy
将此套接字的显式网络代理设为
networkProxy
.
要禁用此套接字所用代理,使用
NoProxy
代理类型:
socket.setProxy(QNetworkProxy.NoProxy)
代理默认值为
DefaultProxy
,意味着套接字将使用应用程序设置:若代理设置采用
setApplicationProxy
,将使用它;否则,若工厂设置采用
setApplicationProxyFactory
, it will query that factory with type
TcpSocket
.
PySide2.QtNetwork.QAbstractSocket.
setReadBufferSize
(
size
)
¶
size
–
qint64
设置尺寸为
QAbstractSocket
‘s internal read buffer to be
size
字节。
若缓冲尺寸被限制到某个大小,
QAbstractSocket
won’t buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.
此选项是有用的若仅在某时间点读取数据 (如:在实时流应用程序中),或者,若希望保护套接字以免接收太多数据 (可能最终导致应用程序内存不足)。
Only
QTcpSocket
使用
QAbstractSocket
‘s internal buffer;
QUdpSocket
does not use any buffering at all, but rather relies on the implicit buffering provided by the operating system. Because of this, calling this function on
QUdpSocket
不起作用。
另请参阅
readBufferSize()
read()
PySide2.QtNetwork.QAbstractSocket.
setSocketDescriptor
(
socketDescriptor
[
,
state=ConnectedState
[
,
openMode=QIODevice.ReadWrite
]
]
)
¶
socketDescriptor
–
qintptr
state
–
SocketState
openMode
–
OpenMode
bool
初始化
QAbstractSocket
采用本机套接字描述符
socketDescriptor
。返回
true
if
socketDescriptor
被接受作为有效套接字描述符;否则返回
false
。以指定模式打开套接字通过
openMode
,并进入指定套接字状态通过
socketState
. Read and write buffers are cleared, discarding any pending data.
注意
It is not possible to initialize two abstract sockets with the same native socket descriptor.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
setSocketError
(
socketError
)
¶
socketError
–
SocketError
Sets the type of error that last occurred to
socketError
.
另请参阅
setSocketState()
setErrorString()
PySide2.QtNetwork.QAbstractSocket.
setSocketOption
(
option
,
value
)
¶
option
–
SocketOption
value – object
设置给定
option
到描述值
value
.
注意
在 Windows Runtime,
KeepAliveOption
must be set before the socket is connected.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
setSocketState
(
state
)
¶
state
–
SocketState
把套接字的状态设为
state
.
另请参阅
PySide2.QtNetwork.QAbstractSocket.
socketDescriptor
(
)
¶
qintptr
返回本地套接字描述符为
QAbstractSocket
对象若这是可用的;否则返回 -1。
If the socket is using
QNetworkProxy
,返回的描述符可能不能用于本机套接字函数。
套接字描述符不可用当
QAbstractSocket
是在
UnconnectedState
.
PySide2.QtNetwork.QAbstractSocket.
socketOption
(
option
)
¶
option
–
SocketOption
object
返回值为
option
选项。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
socketType
(
)
¶
返回套接字类型 (TCP、UDP、或其它)。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
state
(
)
¶
返回套接字的状态。
另请参阅
PySide2.QtNetwork.QAbstractSocket.
stateChanged
(
arg__1
)
¶
arg__1
–
SocketState
PySide2.QtNetwork.QAbstractSocket.
waitForConnected
(
[
msecs=30000
]
)
¶
msecs
–
int
bool
等待直到套接字被连接,最长
msecs
毫秒。若连接已建立,此函数返回
true
;否则它返回
false
。若它返回
false
,可以调用
error()
to determine the cause of the error.
以下范例最多等待 1 秒为建立连接:
socket.connectToHost("imap", 143)
if socket.waitForConnected(1000):
print "Connected!"
若 msecs 为 -1,此函数不会超时。
注意
This function may wait slightly longer than
msecs
, depending on the time it takes to complete the host lookup.
注意
Multiple calls to this functions do not accumulate the time. If the function times out, the connecting process will be aborted.
注意
This function may fail randomly on Windows. Consider using the event loop and the
connected()
signal if your software will run on Windows.
PySide2.QtNetwork.QAbstractSocket.
waitForDisconnected
(
[
msecs=30000
]
)
¶
msecs
–
int
bool
等待直到套接字已断开连接,最长
msecs
毫秒。若连接被成功断开连接,此函数返回
true
;否则它返回
false
(若操作超时,若发生错误,或者若此
QAbstractSocket
已经断开连接)。若它返回
false
,可以调用
error()
to determine the cause of the error.
以下范例最多等待 1 秒为关闭连接:
socket.disconnectFromHost()
if socket.state() == QAbstractSocket.UnconnectedState or \
socket.waitForDisconnected(1000):
print "Disconnected!"
若 msecs 为 -1,此函数不会超时。
注意
This function may fail randomly on Windows. Consider using the event loop and the
disconnected()
signal if your software will run on Windows.
另请参阅
disconnectFromHost()
close()