QUdpSocket Class

翻译自:QT官网文档QUdpSocket类

QUdpSocket类提供一个UDP套接字。

Header: #include <QUdpSocket>
qmake: QT += network
Inherits: QAbstractSocket.
注意:这个类的所有函数都是不可重入的(reentrant)。

所有类的列表,包括继承类

公有函数:



 QUdpSocket(QObject * parent = 0)
virtual ~QUdpSocket()
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)
37公有函数继承自 QAbstractSocket

33公有函数继承自QIODevice

31公有函数继承自QObject


其他的继承的成员:


详细的描述:

QUdpSocket类提供一个UDP套接字。

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 joinMulticastGroup() and leaveMulticastGroup() to control group membership,

and QAbstractSocket::MulticastTtlOption andQAbstractSocket::MulticastLoopbackOption to set the TTL and loopback socket options. UsesetMulticastInterface() to control the outgoing interface for multicast datagrams, andmulticastInterface() to query it.

使用UdpSocket,你可以使用connectToHost()建立一个到UDP服务器的虚拟连接,而且不用指定每条报文的接受者就可以使用read()和write()来交换报文。

The Broadcast SenderBroadcast ReceiverMulticast Sender, and Multicast Receiver examples illustrate how to use QUdpSocket in applications.

See also QTcpSocket.

成员函数文档:

QUdpSocket::QUdpSocket(QObject * parent = 0)

创建一个QUdpSocket对象。

parent继承自QObject的构造函数。

参考socketType()。

QUdpSocket::~QUdpSocket() [virtual]

销毁套接字,如有必要,关闭连接。

参考close()。

bool QUdpSocket::hasPendingDatagrams() const

如果至少有一个报文等到被读,返回true;否则,返回false。

参考pendingDatagramSize()和readDatagram()。

bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress)


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 QHostAddress::Any). You must use QHostAddress::AnyIPv4 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 leaveMulticastGroup().

bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & 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 leaveMulticastGroup().

bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress)

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.

This function was introduced in Qt 4.8.

See also joinMulticastGroup().

bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & 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 joinMulticastGroup().

QNetworkInterface 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 QNetworkInterface. The socket must be in BoundState, otherwise an invalid QNetworkInterface is returned.

This function was introduced in Qt 4.8.

See also setMulticastInterface().



qint64 QUdpSocket::pendingDatagramSize() const

返回第一个待定的报文的大小。如果没有报文待定,返回-1.

参考hasPendingDatagrams()readDatagram()。

qint64 QUdpSocket::readDatagram(char * dataqint64 maxSizeQHostAddress* address = 0, quint16 * port = 0)

接收一个不超过maxsize字节的报文,存到data中。发送者的主机地址和端口号存进*address和*port中(除非指针是0)

返回数据报成功的大小;否则返回-1.

如果maxsize太小,剩下的报文会丢失。为避免数据的丢失,在试图读取之前,调用pendingDatagramSize()来决定待定数据报的大小。如果maxsize是0,数据报将被丢弃。

参考writeDatagram(), hasPendingDatagrams(), and pendingDatagramSize().

void QUdpSocket::setMulticastInterface(const QNetworkInterface & 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 BoundState, otherwise this function does nothing.

This function was introduced in Qt 4.8.

See also multicastInterface(), joinMulticastGroup(), and leaveMulticastGroup().

qint64 QUdpSocket::writeDatagram(const char * dataqint64 size, constQHostAddress & addressquint16 port)

发送size字节大小的数据报给address地址和port端口的主机。返回成功发送的字节数,否则返回-1.

数据报总是被写成一块。数据报的最大大小是高度平台相关的,但是可以低至8192字节。如果报文太大,这个函数会返回-1并且error()会返回DatagramTooLargeError。

发送数据报大于512个字节是不鼓励的,即使成功发送,它们在到达目的地之前也可能被IP层分裂。

警告:在一个连接的UDP套接字上调用这个函数可能产生一个错误,导致没有数据包被发送。如果你使用了连接的套接字,使用write()来发送数据报。

参考readDatagram() and write().

qint64 QUdpSocket::writeDatagram(const QByteArray & datagram, constQHostAddress & hostquint16 port)

这是一个重载函数。

发送datagram数据报给地址是host,端口是port的主机。





  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值