QUdpSocketclass provides a UDP socket. 更多 …
def
hasPendingDatagrams
()
def
joinMulticastGroup
(groupAddress)
def
joinMulticastGroup
(groupAddress, iface)
def
leaveMulticastGroup
(groupAddress)
def
leaveMulticastGroup
(groupAddress, iface)
def
multicastInterface
()
def
pendingDatagramSize
()
def
readDatagram
(, maxlen)
def
receiveDatagram
([maxSize=-1])
def
setMulticastInterface
(iface)
def
writeDatagram
(datagram)
def
writeDatagram
(datagram, host, port)
UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn’t important.
QUdpSocket是子类化的QAbstractSocket,允许发送和接收 UDP 数据报。此类的最常见用法是绑定地址和端口使用
bind(), then callwriteDatagram()andreadDatagram()/receiveDatagram()to transfer data. If you want to use the standardQIODevice函数read(),readLine(),write(), etc., you must first connect the socket directly to a peer by callingconnectToHost().套接字发射
bytesWritten()signal every time a datagram is written to the network. If you just want to send datagrams, you don’t need to callbind().
readyRead()signal is emitted whenever datagrams arrive. In that case,hasPendingDatagrams()返回true。调用pendingDatagramSize()to obtain the size of the first pending datagram, andreadDatagram()orreceiveDatagram()to read it.注意
应该读取传入数据报当接收
readyRead()signal, otherwise this signal will not be emitted for the next datagram.范例:
def initSocket(self): udpSocket = QUdpSocket(self) udpSocket.bind(QHostAddress.LocalHost, 7755) self.connect(udpSocket, SIGNAL('readyRead()'), self, SLOT('readPendingDatagrams()')) def readPendingDatagrams(self): while udpSocket.hasPendingDatagrams(): datagram = QByteArray() datagram.resize(udpSocket.pendingDatagramSize()) (sender, senderPort) = udpSocket.readDatagram(datagram.data(), datagram.size()) processTheDatagram(datagram)
QUdpSocketalso supports UDP multicast. UsejoinMulticastGroup()andleaveMulticastGroup()to control group membership, andMulticastTtlOptionandMulticastLoopbackOption去设置 TTL (生存时间) 和回送套接字选项。使用setMulticastInterface()to control the outgoing interface for multicast datagrams, andmulticastInterface()to query it.With
QUdpSocket, you can also establish a virtual connection to a UDP server usingconnectToHost()and then useread()andwrite()to exchange datagrams without specifying the receiver for each datagram.广播发送器 , 广播接收器 , 多点播送发送器 ,和 多点播送接收者器 examples illustrate how to use
QUdpSocketin applications.
QUdpSocket
(
[
parent=None
]
)
¶
- param parent
QObject
创建
QUdpSocket
对象。
parent
会被传递给
QObject
构造函数。
另请参阅
PySide2.QtNetwork.QUdpSocket.
hasPendingDatagrams
(
)
¶
bool
返回
true
if at least one datagram is waiting to be read; otherwise returns
false
.
PySide2.QtNetwork.QUdpSocket.
joinMulticastGroup
(
groupAddress
)
¶
groupAddress
–
QHostAddress
bool
Joins the multicast group specified by
groupAddress
on the default interface chosen by the operating system. The socket must be in
BoundState
, otherwise an error occurs.
Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using
Any
). You must use
AnyIPv4
代替。
此函数返回
true
if successful; otherwise it returns
false
and sets the socket error accordingly.
注意
Joining IPv6 multicast groups without an interface selection is not supported in all operating systems. Consider using the overload where the interface is specified.
PySide2.QtNetwork.QUdpSocket.
joinMulticastGroup
(
groupAddress
,
iface
)
¶
groupAddress
–
QHostAddress
iface
–
QNetworkInterface
bool
这是重载函数。
Joins the multicast group address
groupAddress
on the interface
iface
.
PySide2.QtNetwork.QUdpSocket.
leaveMulticastGroup
(
groupAddress
)
¶
groupAddress
–
QHostAddress
bool
Leaves the multicast group specified by
groupAddress
on the default interface chosen by the operating system. The socket must be in
BoundState
, otherwise an error occurs.
此函数返回
true
if successful; otherwise it returns
false
and sets the socket error accordingly.
注意
This function should be called with the same arguments as were passed to
joinMulticastGroup()
.
另请参阅
PySide2.QtNetwork.QUdpSocket.
leaveMulticastGroup
(
groupAddress
,
iface
)
¶
groupAddress
–
QHostAddress
iface
–
QNetworkInterface
bool
这是重载函数。
Leaves the multicast group specified by
groupAddress
on the interface
iface
.
注意
This function should be called with the same arguments as were passed to
joinMulticastGroup()
.
另请参阅
PySide2.QtNetwork.QUdpSocket.
multicastInterface
(
)
¶
Returns the interface for the outgoing interface for multicast datagrams. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. If no interface has been previously set, this function returns an invalid
QNetworkInterface
. The socket must be in
BoundState
, otherwise an invalid
QNetworkInterface
被返回。
PySide2.QtNetwork.QUdpSocket.
pendingDatagramSize
(
)
¶
qint64
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.
PySide2.QtNetwork.QUdpSocket.
readDatagram
(
maxlen
)
¶
maxlen
–
qint64
(data, address, port)
Receives a datagram no larger than
maxSize
bytes and stores it in
data
. The sender’s host address and port is stored in *``address`` and *``port`` (unless the pointers are
None
).
Returns the size of the datagram on success; otherwise returns -1.
若
maxSize
is too small, the rest of the datagram will be lost. To avoid loss of data, call
pendingDatagramSize()
to determine the size of the pending datagram before attempting to read it. If
maxSize
is 0, the datagram will be discarded.
PySide2.QtNetwork.QUdpSocket.
receiveDatagram
(
[
maxSize=-1
]
)
¶
maxSize
–
qint64
Receives a datagram no larger than
maxSize
bytes and returns it in the
QNetworkDatagram
object, along with the sender’s host address and port. If possible, this function will also try to determine the datagram’s destination address, port, and the number of hop counts at reception time.
On failure, returns a
QNetworkDatagram
that reports
not
valid
.
若
maxSize
is too small, the rest of the datagram will be lost. If
maxSize
is 0, the datagram will be discarded. If
maxSize
is -1 (the default), this function will attempt to read the entire datagram.
PySide2.QtNetwork.QUdpSocket.
setMulticastInterface
(
iface
)
¶
iface
–
QNetworkInterface
Sets the outgoing interface for multicast datagrams to the interface
iface
. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. The socket must be in
BoundState
, otherwise this function does nothing.
PySide2.QtNetwork.QUdpSocket.
writeDatagram
(
datagram
,
host
,
port
)
¶
datagram
–
QByteArray
host
–
QHostAddress
port
–
quint16
qint64
这是重载函数。
发送数据报
datagram
到主机地址
host
和在端口
port
.
函数返回发送字节数若成功,或 -1 若遭遇错误。
PySide2.QtNetwork.QUdpSocket.
writeDatagram
(
datagram
)
¶
datagram
–
QNetworkDatagram
qint64
这是重载函数。
发送数据报
datagram
到主机地址和端口号包含在
datagram
, using the network interface and hop count limits also set there. If the destination address and port numbers are unset, this function will send to the address that was passed to
connectToHost()
.
若目的地地址是 IPv6 采用非空
scope
id
but differs from the interface index in
datagram
, it is undefined which interface the operating system will choose to send on.
函数返回发送字节数若成功,或 -1 若遭遇错误。
警告
Calling this function on a connected UDP socket may result in an error and no packet being sent. If you are using a connected socket, use
write()
to send datagrams.