QUdpSocket 类提供 UDP (用户数据报协议) 套接字。 更多...
继承 QAbstractSocket .
QUdpSocket 类提供 UDP (用户数据报协议) 套接字。
UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't important. QUdpSocket is a subclass of QAbstractSocket that allows you to send and receive UDP datagrams.
The most common way to use this class is to bind to an address and port using bind (),然后调用 writeDatagram () 和 readDatagram () 到 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 () returns true. Call pendingDatagramSize () 到 obtain the size of the first pending datagram, and readDatagram () 去读取它。
注意: An incoming datagram should be read when you receive the readyRead () signal, otherwise this signal will not be emitted for the next datagram.
范例:
void Server.initSocket() { udpSocket = new QUdpSocket(this); udpSocket->bind(QHostAddress.LocalHost, 7755); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); } void Server.readPendingDatagrams() { while (udpSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(udpSocket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort); processTheDatagram(datagram); } }
QUdpSocket 还支持 UDP 多点播送。使用 joinMulticastGroup () 和 leaveMulticastGroup () 到 control group membership, and QAbstractSocket.MulticastTtlOption and QAbstractSocket.MulticastLoopbackOption 去设置 TTL (生存时间) 和回送套接字选项。使用 setMulticastInterface () to control the outgoing interface for multicast datagrams, and multicastInterface () 到 query it.
With QUdpSocket, you can also establish a virtual connection to a UDP server using connectToHost () and then use read () 和 write () to exchange datagrams without specifying the receiver for each datagram.
广播发送器 , 广播接收器 , 多点播送发送器 ,和 多点播送接收者器 范例阐明如何在应用程序中使用 QUdpSocket。
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.
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 | 0x1 | 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 | 0x2 | 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 | 0x4 | Provides a hint to 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 | 0x0 | 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. |
该枚举在 Qt 4.1 引入或被修改。
BindMode 类型是 typedef 对于 QFlags <BindFlag>. It stores an OR combination of BindFlag values.
parent argument, if not None, causes self to be owned by Qt instead of PyQt.
创建 QUdpSocket 对象。
parent 会被传递给 QObject 构造函数。
另请参阅 socketType ().
Binds this socket to the address address and the port port . When bound, the signal 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 .
另请参阅 readDatagram ().
这是重载函数。
Binds to address 在端口 port ,使用 BindMode mode .
该函数在 Qt 4.1 引入。
这是重载函数。
Binds to QHostAddress :Any on port port .
这是重载函数。
Binds to QHostAddress :Any on port port ,使用 BindMode mode .
该函数在 Qt 4.1 引入。
Returns true if at least one datagram is waiting to be read; otherwise returns false.
另请参阅 pendingDatagramSize () 和 readDatagram ().
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.
该函数在 Qt 4.8 引入。
另请参阅 leaveMulticastGroup ().
这是重载函数。
Joins the multicast group address groupAddress 在 interface iface .
该函数在 Qt 4.8 引入。
另请参阅 leaveMulticastGroup ().
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.
该函数在 Qt 4.8 引入。
另请参阅 joinMulticastGroup ().
这是重载函数。
Leaves the multicast group specified by groupAddress on the interface iface .
该函数在 Qt 4.8 引入。
另请参阅 joinMulticastGroup ().
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 被返回。
该函数在 Qt 4.8 引入。
另请参阅 setMulticastInterface ().
Returns the size of the first pending UDP datagram. If there is no datagram available, this function returns -1.
另请参阅 hasPendingDatagrams () 和 readDatagram ().
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 pendingDatagramSize () 到 determine the size of the pending datagram before attempting to read it. If maxSize is 0, the datagram will be discarded.
另请参阅 writeDatagram (), hasPendingDatagrams (), and pendingDatagramSize ().
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.
该函数在 Qt 4.8 引入。
另请参阅 multicastInterface (), joinMulticastGroup (),和 leaveMulticastGroup ().
Sends the datagram at data of size size 到 host address address 在端口 port . Returns the number of bytes sent on success; otherwise returns -1.
Datagrams are always written as one block. The maximum size of a datagram is highly platform-dependent, but can be as low as 8192 bytes. If the datagram is too large, this function will return -1 and error () 会返回 DatagramTooLargeError .
Sending datagrams larger than 512 bytes is in general disadvised, as even if they are sent successfully, they are likely to be fragmented by the IP layer before arriving at their final destination.
警告: In S60 5.0 and earlier versions, the writeDatagram return value is not reliable for large datagrams.
警告: 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 () 到 send datagrams.
另请参阅 readDatagram () 和 write ().
这是重载函数。
Sends the datagram datagram to the host address host and at port port .