四十六、Qt网络(六)UDP
?
像QQ那樣,當有很多用戶,發(fā)送的大部分都是短消息,要求能及時響應,并且對安全性要求不是很高的情況下使用UDP協(xié)議。發(fā)送(客戶請求發(fā)送數(shù)據(jù))
很簡單,僅需QUdpSocket 的writeDatagram函數(shù)即可
[cpp]?view plaincopy
接收(服務器端監(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
[cpp]?view plaincopy
[cpp]?view plaincopy
用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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mfs教程(四)
- 下一篇: javaSE-多线程1