PySide.QtNetwork.QUdpSocket class provides a UDP socket.
UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't important. PySide.QtNetwork.QUdpSocket 是子类化的 PySide.QtNetwork.QAbstractSocket ,允许发送和接收 UDP 数据报。
此类的最常见用法是绑定地址和端口使用 PySide.QtNetwork.QUdpSocket.bind() , then call PySide.QtNetwork.QUdpSocket.writeDatagram() and PySide.QtNetwork.QUdpSocket.readDatagram() to transfer data. If you want to use the standard PySide.QtCore.QIODevice 函数 PySide.QtCore.QIODevice.read() , PySide.QtCore.QIODevice.readLine() , PySide.QtCore.QIODevice.write() , etc., you must first connect the socket directly to a peer by calling PySide.QtNetwork.QAbstractSocket.connectToHost() .
套接字发射 PySide.QtCore.QIODevice.bytesWritten() signal every time a datagram is written to the network. If you just want to send datagrams, you don't need to call PySide.QtNetwork.QUdpSocket.bind() .
PySide.QtCore.QIODevice.readyRead() signal is emitted whenever datagrams arrive. In that case, PySide.QtNetwork.QUdpSocket.hasPendingDatagrams() returns true. Call PySide.QtNetwork.QUdpSocket.pendingDatagramSize() to obtain the size of the first pending datagram, and PySide.QtNetwork.QUdpSocket.readDatagram() to read it.
注意
应该读取传入数据报当接收 PySide.QtCore.QIODevice.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)
PySide.QtNetwork.QUdpSocket also supports UDP multicast. Use PySide.QtNetwork.QUdpSocket.joinMulticastGroup() and PySide.QtNetwork.QUdpSocket.leaveMulticastGroup() to control group membership, and QAbstractSocket.MulticastTtlOption and QAbstractSocket.MulticastLoopbackOption 去设置 TTL (生存时间) 和回送套接字选项。使用 PySide.QtNetwork.QUdpSocket.setMulticastInterface() to control the outgoing interface for multicast datagrams, and PySide.QtNetwork.QUdpSocket.multicastInterface() to query it.
With PySide.QtNetwork.QUdpSocket , you can also establish a virtual connection to a UDP server using PySide.QtNetwork.QAbstractSocket.connectToHost() and then use PySide.QtCore.QIODevice.read() and PySide.QtCore.QIODevice.write() to exchange datagrams without specifying the receiver for each datagram.
广播发送器 , 广播接收器 , 多点播送发送器 ,和 多点播送接收者器 examples illustrate how to use PySide.QtNetwork.QUdpSocket in applications.
On Symbian, processes which use this class must have the NetworkServices platform security capability. If the client process lacks this capability, operations will result in a panic.
Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.
| 参数: | parent – PySide.QtCore.QObject |
|---|
创建 PySide.QtNetwork.QUdpSocket 对象。
parent 会被传递给 PySide.QtCore.QObject 构造函数。
This enum describes the different flags you can pass to modify the behavior of QUdpSocket.bind() .
注意
On Symbian OS bind flags behaviour depends on process capabilties. If process has NetworkControl capability, the bind attempt with ReuseAddressHint will always succeed even if the address and port is already bound by another socket with any flags. If process does not have NetworkControl capability, the bind attempt to address and port already bound by another socket will always fail.
| 常量 | 描述 |
|---|---|
| QUdpSocket.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 ReuseAddressHint , 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 option is ignored. |
| QUdpSocket.DontShareAddress | Bind the address and port exclusively, so that no other services are allowed to rebind. By passing this option to QUdpSocket.bind() , you are guaranteed that on successs, your service is the only one that listens to the address and port. No services are allowed to rebind, even if they pass ReuseAddressHint . This option provides more security than ShareAddress , but on certain operating systems, it requires you to run the server with administrator privileges. On Unix and Mac OS X, not sharing is the default behavior for binding an address and port, so this option is ignored. On Windows, this option uses the SO_EXCLUSIVEADDRUSE socket option. |
| QUdpSocket.ReuseAddressHint | Provides a hint to PySide.QtNetwork.QUdpSocket that it should try to rebind the service even if the address and port are already bound by another socket. On Windows, this is equivalent to the SO_REUSEADDR socket option. On Unix, this option is ignored. |
| QUdpSocket.DefaultForPlatform | The default option for the current platform. On Unix and Mac OS X, this is equivalent to ( DontShareAddress + ReuseAddressHint ), and on Windows, its equivalent to ShareAddress . |
| 参数: | port – PySide.QtCore.quint16 |
|---|---|
| 返回类型: | PySide.QtCore.bool |
这是重载函数。
Binds to PySide.QtNetwork.QHostAddress :Any on port port .
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
Binds this socket to the address address and the port port . When bound, the signal PySide.QtCore.QIODevice.readyRead() is emitted whenever a UDP datagram arrives on the specified address and port. This function is useful to write UDP servers.
On success, the functions returns true and the socket enters BoundState ; otherwise it returns false.
The socket is bound using the DefaultForPlatform BindMode .
| 返回类型: | PySide.QtCore.bool |
|---|
Returns true if at least one datagram is waiting to be read; otherwise returns false.
| 参数: | groupAddress – PySide.QtNetwork.QHostAddress |
|---|---|
| 返回类型: | PySide.QtCore.bool |
Joins the 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.
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
Joins the multicast group address groupAddress on the interface iface .
| 参数: | groupAddress – PySide.QtNetwork.QHostAddress |
|---|---|
| 返回类型: | PySide.QtCore.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.
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
这是重载函数。
Leaves the multicast group specified by groupAddress on the interface iface .
| 返回类型: | PySide.QtNetwork.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 PySide.QtNetwork.QNetworkInterface . The socket must be in BoundState , otherwise an invalid PySide.QtNetwork.QNetworkInterface 被返回。
| 返回类型: | PySide.QtCore.qint64 |
|---|
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.
| 参数: | maxlen – PySide.QtCore.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 0).
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 PySide.QtNetwork.QUdpSocket.pendingDatagramSize() to determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.
| 参数: | iface – PySide.QtNetwork.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.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.qint64 |
这是重载函数。
发送数据报 datagram 到主机地址 host 和在端口 port .