翻译自:
QUdpSocket类提供一个UDP套接字。
Header: | #include <QUdpSocket> |
qmake: | QT += network |
Inherits: | . |
公有函数:
(QObject * parent = 0) | |
virtual | () |
bool | () const |
bool | (const QHostAddress & groupAddress) |
bool | (const QHostAddress & groupAddress, const QNetworkInterface & iface) |
bool | (const QHostAddress & groupAddress) |
bool | (const QHostAddress & groupAddress, const QNetworkInterface & iface) |
QNetworkInterface | () const |
qint64 | () const |
qint64 | (char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0) |
void | (const QNetworkInterface & iface) |
qint64 | (const char * data, qint64 size, const QHostAddress & address, quint16 port) |
qint64 | (const QByteArray & datagram, const QHostAddress & host, quint16 port) |
33公有函数继承自QIODevice
31公有函数继承自QObject
其他的继承的成员:
- 1 property inherited from
- 1 public slot inherited from
- 6 signals inherited from
- 4 signals inherited from
- 2 signals inherited from
- 1 public variable inherited from
- 10 static public members inherited from
- 10 protected functions inherited from
- 5 protected functions inherited from
- 9 protected functions inherited from
- 2 protected variables inherited from
UDP(用户数据报协议)是一个轻量级的,不可靠的,面向数据包的无连接的协议。当可靠性不重要的时候,可以使用UDP。QUdpSocket是QAbstractSocket的一个子类,可以用来收发数据报。
这个类最多的用途就是使用bind()绑定一个地址和端口,然后调用writeDatagram()和readDatagram()来传递数据。如果你想用标准QIODevice类的方法read(),readLine(),write()等,你必须首先直接连接到套接字,通过调用connectToHost()函数连接一个同等的用户(peer)。 每次有数据报写到网络中时,套接字发射bytesWritten()信号。如果你只是想发送数据报,你不需要调用bind()函数。 当数据报到达的时候,readyRead()信号被发射。这样,hasPendingDatagrams()返回true。调用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 also supports UDP multicast. Use () and () to control group membership,
and and to set the TTL and loopback socket options. Use() to control the outgoing interface for multicast datagrams, and() to query it.
使用UdpSocket,你可以使用connectToHost()建立一个到UDP服务器的虚拟连接,而且不用指定每条报文的接受者就可以使用read()和write()来交换报文。
The , , , and examples illustrate how to use QUdpSocket in applications.
See also .
成员函数文档:
QUdpSocket::QUdpSocket( * parent = 0)
创建一个QUdpSocket对象。parent继承自QObject的构造函数。
参考socketType()。
QUdpSocket::~QUdpSocket() [virtual]
销毁套接字,如有必要,关闭连接。参考close()。
bool QUdpSocket::hasPendingDatagrams() const
如果至少有一个报文等到被读,返回true;否则,返回false。参考pendingDatagramSize()和readDatagram()。
bool QUdpSocket::joinMulticastGroup(const & groupAddress)
Joins the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in , 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 ). You must use instead.
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
This function was introduced in Qt 4.8.
See also ().
bool QUdpSocket::joinMulticastGroup(const & groupAddress, const & iface)
This is an overloaded function.
Joins the multicast group address groupAddress on the interface iface.
This function was introduced in Qt 4.8.
See also ().
bool QUdpSocket::leaveMulticastGroup(const & groupAddress)
Leaves the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in , otherwise an error occurs.
This function returns true if successful; otherwise it returns false and sets the socket error accordingly.
This function was introduced in Qt 4.8.
See also ().
bool QUdpSocket::leaveMulticastGroup(const & groupAddress, const & iface)
This is an overloaded function.
Leaves the multicast group specified by groupAddress on the interface iface.
This function was introduced in Qt 4.8.
See also ().
QUdpSocket::multicastInterface() const
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 . The socket must be in , otherwise an invalid is returned.
This function was introduced in Qt 4.8.
See also ().
qint64 QUdpSocket::pendingDatagramSize() const
返回第一个待定的报文的大小。如果没有报文待定,返回-1.参考hasPendingDatagrams()readDatagram()。
qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, * address = 0, quint16 * port = 0)
接收一个不超过maxsize字节的报文,存到data中。发送者的主机地址和端口号存进*address和*port中(除非指针是0)返回数据报成功的大小;否则返回-1.
如果maxsize太小,剩下的报文会丢失。为避免数据的丢失,在试图读取之前,调用pendingDatagramSize()来决定待定数据报的大小。如果maxsize是0,数据报将被丢弃。
参考(), (), and ().
void QUdpSocket::setMulticastInterface(const & iface)
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 , otherwise this function does nothing.
This function was introduced in Qt 4.8.
See also (), (), and ().
qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const & address, quint16 port)
发送size字节大小的数据报给address地址和port端口的主机。返回成功发送的字节数,否则返回-1.数据报总是被写成一块。数据报的最大大小是高度平台相关的,但是可以低至8192字节。如果报文太大,这个函数会返回-1并且error()会返回DatagramTooLargeError。
发送数据报大于512个字节是不鼓励的,即使成功发送,它们在到达目的地之前也可能被IP层分裂。
警告:在一个连接的UDP套接字上调用这个函数可能产生一个错误,导致没有数据包被发送。如果你使用了连接的套接字,使用write()来发送数据报。
参考() and ().
qint64 QUdpSocket::writeDatagram(const & datagram, const & host, quint16 port)
这是一个重载函数。发送datagram数据报给地址是host,端口是port的主机。