内容表

上一话题

QTcpSocket

下一话题

PySide2.QtOpenGL

QUdpSocket

QUdpSocket class provides a UDP socket. 更多

Inheritance diagram of PySide2.QtNetwork.QUdpSocket

概要

函数

详细描述

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 call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice 函数 read() , readLine() , write() , etc., you must first connect the socket directly to a peer by calling connectToHost() .

套接字发射 bytesWritten() signal every time a datagram is written to the network. If you just want to send datagrams, you don’t need to call bind() .

readyRead() signal is emitted whenever datagrams arrive. In that case, hasPendingDatagrams() 返回 true 。调用 pendingDatagramSize() to obtain the size of the first pending datagram, and readDatagram() or receiveDatagram() 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)
											

QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and MulticastTtlOption and MulticastLoopbackOption 去设置 TTL (生存时间) 和回送套接字选项。使用 setMulticastInterface() to control the outgoing interface for multicast datagrams, and multicastInterface() to query it.

With QUdpSocket , you can also establish a virtual connection to a UDP server using connectToHost() and then use read() and write() to exchange datagrams without specifying the receiver for each datagram.

广播发送器 , 广播接收器 , 多点播送发送器 ,和 多点播送接收者器 examples illustrate how to use QUdpSocket in applications.

class QUdpSocket ( [ parent=None ] )
param parent

QObject

创建 QUdpSocket 对象。

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

另请参阅

socketType()

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 )
参数
返回类型

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 )
参数
返回类型

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 ( )
返回类型

QNetworkInterface

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

返回类型

QNetworkDatagram

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 )
参数
返回类型

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.