:使用QUdpSocket进行UDP通讯

1、QUdpSocket类用法介绍

(如下内容为QT帮助文档中的原文)服务器

The QUdpSocket class provides a UDP socket.网络

QUdpSocket类提供了一个UDP socket套接字app

 

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.less

UDP(用户数据报协议),是一个轻量的,不可靠的,面向数据报的,非链接的协议。当通信要求的可靠性不重要时(可能数据会丢失),能够用UDP通讯。QUdpSocket是QAbstractSocket的一个子类,它容许你发送和接收UDP数据报。socket

 

The most common way to use this class is to bind to an address and port using bind(), then call writeDatagram() and readDatagram() / receiveDatagram() to transfer data. If you want to use the standard QIODevice functions read(), readLine(), write(), etc., you must first connect the socket directly to a peer by calling connectToHost().ide

最多见的使用方式是用这个类中的函数 bind(),去绑定一个地址和端口,而后调用 writeDatagram() 和 readDatagram() / receiveDatagram() 去传输数据,若是你想用标准的 QIODevice 函数 read(), readLine(), write(),等,你必须先调用 connectToHost() 去链接socket到另外一端。函数

 

The socket emits the 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().oop

当一个数据报被写进了网络中,socket就会发射一个bytesWritten()信号。若是你只是想发数据,不接收数据,那你就不必调用 bind()。ui

 

The readyRead() signal is emitted whenever datagrams arrive. In that case, hasPendingDatagrams() returns true. Call pendingDatagramSize() to obtain the size of the first pending datagram, and readDatagram() or receiveDatagram() to read it.this

不管何时,当有数据报到达,就会有一个叫 readyRead() 的信号被发出。在这种状况下,hasPendingDatagrams() 会返回一个true。调用 pendingDatagramSize() 去获取第一条在接收队列中等待取出的数据报的字节数,调用 readDatagram() 或 receiveDatagram() 能够读取数据报。

 

Note: An incoming datagram should be read when you receive the readyRead() signal, otherwise this signal will not be emitted for the next datagram.

注意:当你接收到 readyRead() 信号,一条刚到达的数据报就应要被读取,不然该信号在下个数据报到来时就不会被发射。

 

Example:

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()) { QNetworkDatagram datagram = udpSocket->receiveDatagram(); processTheDatagram(datagram); } }

 

QUdpSocket also supports UDP multicast. Use joinMulticastGroup() and leaveMulticastGroup() to control group membership, and QAbstractSocket::MulticastTtlOption and QAbstractSocket::MulticastLoopbackOption to set the TTL and loopback socket options. Use setMulticastInterface() to control the outgoing interface for multicast datagrams, and multicastInterface() to query it.

QUdpSocket类也支持UDP组播(组播:Server能够将数据包只发送给指定组内的客户端,而不发送给指定组外的客户端,也称多播)。能够用 joinMulticastGroup() 和 leaveMulticastGroup() 去控制管理组内的成员关系,QAbstractSocket::MulticastTtlOption 和 QAbstractSocket::MulticastLoopbackOption 能够设置TTL(Time To Live生存时间值)和socket回送选项。调用 setMulticastInterface() 能够为组播数据报管理外部接口,用 multicastInterface() 去查询。

 

With QUdpSocket, you can also establish a virtual connection to a UDP server using connectToHost() and then use read() and write() to exchange datagrams without specifying the receiver for each datagram.

对于QUdpSocket,你也能够用 connectToHost() 与UDP服务器创建一个虚拟链接,而后用 read() 和 write()去交换数据报,而无需为每一个数据报指定接收器。

 

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

若是在应用中使用QUdpSocket,在广播发送器,广播接收器,多播发送器和多播接收器的例子中都有说明。

 

2、QUdpSocket实际应用

一、udp服务端

  功能:一、接收来自客户端的数据  二、给客户端回送相关数据

  步骤:

    一、在.pro文件中添加网络模块  

QT += core gui network

    二、new一个QUdpSocket对象,并绑定端口号,为 readyRead() 链接信号槽

QUdpSocket *udpSocket = new QUdpSocket; bool result = udpSocket->bind(29200); qDebug()<<"#### socket bind result:"<<result<<" #####"; connect(udpSocket, SIGNAL(readyRead()), this, SLOT(recvData()));

    三、在槽函数中用 hasPendingDatagrams() 和 readDatagram() 接收数据

void MainWindow::recvData() {   while(udpSocket->hasPendingDatagrams())   {     QHostAddress srcAddress;     QByteArray datagram; 
    datagram.resize(udpSocket
->pendingDatagramSize());     udpSocket->readDatagram(datagram.data(), datagram.size(),&srcAddress);     QString msg = datagram.data();     qDebug()<<msg<<" ## "<<srcAddress;   } }

    四、用 writeDatagram() 发送数据

char data[2] = {0x01,0x02}; size = 2; udpSocket->writeDatagram(data,size, QHostAddress("123.123.123.123"), 29234); //要发送的目标ip和端口

二、udp客户端

  功能:一、给服务端发送相关数据  二、接收服务端回送相关数据

  从功能上看是彻底与服务端同样的,所以实现方法也和服务端一致,只是绑定的端口是服务端调用 writeDatagram() 中的端口,发送的端口是服务端绑定的端口。

相关文章
相关标签/搜索