日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

四十六、Qt网络(六)UDP

發(fā)布時間:2024/4/17 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四十六、Qt网络(六)UDP 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

像QQ那樣,當有很多用戶,發(fā)送的大部分都是短消息,要求能及時響應,并且對安全性要求不是很高的情況下使用UDP協(xié)議。

發(fā)送(客戶請求發(fā)送數(shù)據(jù))
很簡單,僅需QUdpSocket 的writeDatagram函數(shù)即可
[cpp]?view plaincopy
  • void?Widget::on_pushButton_clicked()??
  • {???QUdpSocket?*sender;??
  • ????sender?=?new?QUdpSocket(this);??
  • ????QByteArray?datagram?=?"hello?world!";??
  • ????sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress("192.168.1.100"),45454);??
  • ????//sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,45454);??
  • ???//?sender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::LocalHost,45454);??
  • ????delete?sender;??
  • }??

  • 接收(服務器端監(jiān)聽)
    使用QUdpSocket 的bind函數(shù)監(jiān)聽某個端口
    當監(jiān)聽的端口有數(shù)據(jù)到達時,QUdpSocket 的信號readyRead()就emit,然后在對應的槽函數(shù)里使用QUdpSocket 的readDatagram讀取數(shù)據(jù)

    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 plaincopy
  • private:??
  • ????QUdpSocket?*receiver;??
  • private?slots:??
  • ????void?processPendingDatagram();??

  • [cpp]?view plaincopy
  • receiver?=?new?QUdpSocket(this);??
  • receiver->bind(45454,QUdpSocket::ShareAddress);??
  • connect(receiver,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));??

  • [cpp]?view plaincopy
  • void?Widget::processPendingDatagram()?//處理等待的數(shù)據(jù)報??
  • {??
  • ????while(receiver->hasPendingDatagrams())??//擁有等待的數(shù)據(jù)報??
  • ????{??
  • ????????QByteArray?datagram;?//擁于存放接收的數(shù)據(jù)報??
  • ????????datagram.resize(receiver->pendingDatagramSize());??
  • ????????//讓datagram的大小為等待處理的數(shù)據(jù)報的大小,這樣才能接收到完整的數(shù)據(jù)??
  • ????????receiver->readDatagram(datagram.data(),datagram.size());??
  • ????????//接收數(shù)據(jù)報,將其存放到datagram中??
  • ????????ui->label->setText(datagram);??
  • ????????//將數(shù)據(jù)報內容顯示出來??
  • ????}??
  • }??



  • 用wireshark監(jiān)聽xp 192.168.1.100和虛擬機fedora 192.168.1.103之間的udp數(shù)據(jù)包,如下
    ????????????????????????????????????? hello world
    xp 192.168.1.100------------------------->fedora 192.168.1.103???
    (wireshark操作:capture->Options里選擇要監(jiān)視的網(wǎng)卡,然后點Start。可以選擇capture/capture filters然后選擇udp only過濾一下)


    雙擊進入詳細數(shù)據(jù)


    可見udp Frame的層層包裹,
    第1層EtherNet II包,記錄源MAC和目的MAC等
    第2層是IPv4包,記錄源ip和目的ip等
    第3層是udp包,記錄端口等
    第4層才是真正的數(shù)據(jù),"hello world"

    ???????????????????????? ? ? ? ? ? ? ? ? ??? hello world ??????????
    fedora 192.168.1.103------------------------->xp 192.168.1.100?
    和上面一樣的,源和目的ip換了一下
    分享到:?

    ?

    轉載于:https://www.cnblogs.com/MingZznet/articles/3211004.html

    總結

    以上是生活随笔為你收集整理的四十六、Qt网络(六)UDP的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。