The QUdpSocket 类提供 UDP (用户数据报协议) 套接字。 更多...
| 头: | #include <QUdpSocket> |
| 继承: | QAbstractSocket |
注意: 此类的所有函数 可重入 .
| enum | BindFlag { ShareAddress, DontShareAddress, ReuseAddressHint, DefaultForPlatform } |
| flags | BindMode |
| QUdpSocket (QObject * parent = 0) | |
| virtual | ~QUdpSocket () |
| bool | bind (const QHostAddress & address , quint16 port ) |
| bool | bind (const QHostAddress & address , quint16 port , BindMode mode ) |
| bool | bind (quint16 port = 0) |
| bool | bind (quint16 port , BindMode mode ) |
| bool | hasPendingDatagrams () const |
| bool | joinMulticastGroup (const QHostAddress & groupAddress ) |
| bool | joinMulticastGroup (const QHostAddress & groupAddress , const QNetworkInterface & iface ) |
| bool | leaveMulticastGroup (const QHostAddress & groupAddress ) |
| bool | leaveMulticastGroup (const QHostAddress & groupAddress , const QNetworkInterface & iface ) |
| QNetworkInterface | multicastInterface () const |
| qint64 | pendingDatagramSize () const |
| qint64 | readDatagram (char * data , qint64 maxSize , QHostAddress * address = 0, quint16 * port = 0) |
| void | setMulticastInterface (const QNetworkInterface & iface ) |
| qint64 | writeDatagram (const char * data , qint64 size , const QHostAddress & address , quint16 port ) |
| qint64 | writeDatagram (const QByteArray & datagram , const QHostAddress & host , quint16 port ) |
The QUdpSocket 类提供 UDP (用户数据报协议) 套接字。
UDP (用户数据报协议) 是轻量、不可靠、面向数据报的无连接协议。可以使用它,当可靠性不重要时。 QUdpSocket 是子类化的 QAbstractSocket ,允许发送和接收 UDP 数据报。
此类的最常见用法是绑定地址和端口使用 bind (),然后调用 writeDatagram () 和 readDatagram () 去传输数据。若想要使用标准 QIODevice 函数 read (), readLine (), write () 等,必须首先把套接字直接连接到对等方,通过调用 connectToHost ().
套接字发射 bytesWritten () 信号,每当数据报被写入网络时。若仅仅希望发送数据报,不需要调用 bind ().
The readyRead () 信号被发射,每当数据报到达时。在此情况下, hasPendingDatagrams () returns true. Call pendingDatagramSize () 以获得第一待决数据报的尺寸,和 readDatagram () 去读取它。
注意: 应该读取传入数据报当收到 readyRead () 信号时;否则下一数据报不会发射此信号。
范例:
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 () 去控制组成员资格,和 QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption 去设置 TTL (生存时间) 和回送套接字选项。使用 setMulticastInterface () 去控制多点播送数据报的传出接口,和 multicastInterface () 去查询它。
采用 QUdpSocket ,还可以建立到 UDP 服务器的虚拟连接使用 connectToHost () 然后使用 read () 和 write () 去交换数据,不为每数据报指定接收者。
The 广播发送器 , 广播接收器 , 多点播送发送器 ,和 多点播送接收器 范例阐明如何使用 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.
另请参阅 QTcpSocket .
此枚举描述的不同标志可以传递给行为修改 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
|
允许将其它服务绑定到相同地址和端口。这很有用,当多个进程通过监听相同地址和端口共享单个服务的负载时 (如:带有几个预分叉监听器的 web 服务器可以大大改进响应时间)。不管怎样,因为允许重新绑定任何服务,所以此选项服从某些安全性注意事项。注意,通过组合此选项与 ReuseAddressHint,还允许服务重新绑定现有共享地址。在 Unix,这相当于 SO_REUSEADDR 套接字选项。在 Windows,此选项被忽略。 |
QUdpSocket::DontShareAddress
|
0x2
|
独占绑定地址和端口,所以不允许重新绑定其它服务。通过将此选项传递给 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
|
提供提示为 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>。它存储 BindFlag 值的 OR 组合。
创建 QUdpSocket 对象。
parent 被传递给 QObject 构造函数。
另请参阅 socketType ().
[虚拟]
QUdpSocket::
~QUdpSocket
()
销毁套接字,关闭连接 (若有必要)。
另请参阅 close ().
将此套接字绑定到地址 address 和端口 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 ().
这是重载函数。
绑定到 address 在端口 port ,使用 BindMode mode .
该函数在 Qt 4.1 引入。
这是重载函数。
绑定到 QHostAddress :任何在端口 port .
这是重载函数。
绑定到 QHostAddress :任何在端口 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 在由操作系统选择的默认接口。套接字必须处于 BoundState ,否则出现错误。
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
该函数在 Qt 4.8 引入。
另请参阅 leaveMulticastGroup ().
这是重载函数。
加入多点播送组地址 groupAddress 按接口 iface .
该函数在 Qt 4.8 引入。
另请参阅 leaveMulticastGroup ().
离开多点播送组指定通过 groupAddress 在由操作系统选择的默认接口。套接字必须处于 BoundState ,否则出现错误。
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
该函数在 Qt 4.8 引入。
另请参阅 joinMulticastGroup ().
这是重载函数。
离开多点播送组指定通过 groupAddress 按接口 iface .
该函数在 Qt 4.8 引入。
另请参阅 joinMulticastGroup ().
返回用于多点播送数据报的传出接口的接口。这相当于 IPv4 套接字的 IP_MULTICAST_IF 套接字选项和 IPv6 套接字的 IPV6_MULTICAST_IF 套接字选项。若先前没有设置接口,此函数返回无效 QNetworkInterface 。套接字必须处于 BoundState ,否则无效 QNetworkInterface 被返回。
该函数在 Qt 4.8 引入。
另请参阅 setMulticastInterface ().
返回第一待决 UDP 数据报的大小。若没有可用数据报,此函数返回 -1。
另请参阅 hasPendingDatagrams () 和 readDatagram ().
接收数据报不大于 maxSize 字节并把它存储在 data 。发送器的主机地址和端口是存储在 * address 和 * port (除非指针为 0)。
返回数据报的大小当成功时;否则返回 -1。
若 maxSize 太小,其余数据报将丢失。为避免数据丢失,调用 pendingDatagramSize () 以确定待决数据报的大小在试图读取它之前。若 maxSize 为 0,数据报会被丢弃。
另请参阅 writeDatagram (), hasPendingDatagrams (),和 pendingDatagramSize ().
将多点播送数据报的传出接口设为接口 iface 。这相当于 IPv4 套接字的 IP_MULTICAST_IF 套接字选项和 IPv6 套接字的 IPV6_MULTICAST_IF 套接字选项。套接字必须处于 BoundState ,否则此函数什么都不做。
该函数在 Qt 4.8 引入。
另请参阅 multicastInterface (), joinMulticastGroup (),和 leaveMulticastGroup ().
发送数据报按 data 的大小 size 到主机地址 address 在端口 port 。返回发送字节数当成功时;否则返回 -1。
数据报始终作为一个块写入。数据报的最大大小高度从属平台,但可以低至 8192 字节。若数据报太大,此函数将返回 -1 和 error () 会返回 DatagramTooLargeError .
一般不建议发送大于 512 字节的数据报,因为即使成功发送,它们也可能被 IP 层碎片化在抵达其最终目的地之前。
警告: In S60 5.0 and earlier versions, the writeDatagram return value is not reliable for large datagrams.
警告: 在已连接的 UDP 套接字中调用此函数,可能导致错误且不发送数据包。若正使用已连接的套接字,使用 write () 来发送数据报。
另请参阅 readDatagram () 和 write ().
这是重载函数。
发送数据报 datagram 到主机地址 host 和在端口 port .