Qt网络(六)UDP

 

像QQ那样,当有很多用户,发送的大部分都是短消息,要求能及时响应,并且对安全性要求不是很高的情况下使用UDP协议。

发送(客户请求发送数据)
很简单,仅需QUdpSocket 的writeDatagram函数即可
[cpp]   view plain copy
  1. void Widget::on_pushButton_clicked()  
  2. {   QUdpSocket *sender;  
  3.     sender = new QUdpSocket(this);  
  4.     QByteArray datagram = "hello world!";  
  5.     sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress("192.168.1.100"),45454);  
  6.     //sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,45454);  
  7.    // sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::LocalHost,45454);  
  8.     delete sender;  
  9. }  

接收(服务器端监听)
使用QUdpSocket 的bind函数监听某个端口
当监听的端口有数据到达时,QUdpSocket 的信号readyRead()就emit,然后在对应的槽函数里使用QUdpSocket 的readDatagram读取数据

void QIODevice::readyRead () [signal]

This signal is emitted once every time new data is available for reading from the device. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.

readyRead() is not emitted recursively; if you reenter the event loop or call waitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (although waitForReadyRead() may still return true).

Note for developers implementing classes derived from QIODevice: you should always emit readyRead() when new data has arrived (do not emit it only because there's data still to be read in your buffers). Do not emit readyRead() in other conditions.


[cpp]   view plain copy
  1. private:  
  2.     QUdpSocket *receiver;  
  3. private slots:  
  4.     void processPendingDatagram();  

[cpp]   view plain copy
  1. receiver = new QUdpSocket(this);  
  2. receiver->bind(45454,QUdpSocket::ShareAddress);  
  3. connect(receiver,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));  

[cpp]   view plain copy
  1. void Widget::processPendingDatagram() //处理等待的数据报  
  2. {  
  3.     while(receiver->hasPendingDatagrams())  //拥有等待的数据报  
  4.     {  
  5.         QByteArray datagram; //拥于存放接收的数据报  
  6.         datagram.resize(receiver->pendingDatagramSize());  
  7.         //让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据  
  8.         receiver->readDatagram(datagram.data(),datagram.size());  
  9.         //接收数据报,将其存放到datagram中  
  10.         ui->label->setText(datagram);  
  11.         //将数据报内容显示出来  
  12.     }  
  13. }  


用wireshark监听xp 192.168.1.100和虚拟机fedora 192.168.1.103之间的udp数据包,如下
                                      hello world
xp 192.168.1.100------------------------->fedora 192.168.1.103   
(wireshark操作:capture->Options里选择要监视的网卡,然后点Start。可以选择capture/capture filters然后选择udp only过滤一下)

Qt网络(六)UDP_第1张图片

可见udp Frame的层层包裹,
第1层EtherNet II包,记录源MAC和目的MAC等
第2层是IPv4包,记录源ip和目的ip等
第3层是udp包,记录端口等
第4层才是真正的数据,"hello world"

                                             hello world           
fedora 192.168.1.103------------------------->xp 192.168.1.100 
和上面一样的,源和目的ip换了一下

Qt网络(六)UDP_第2张图片

你可能感兴趣的:(虚拟机,网络,socket,qt,NetWork,Signal)