四十六、Qt网络(六)UDP
?
像QQ那樣,當有很多用戶,發送的大部分都是短消息,要求能及時響應,并且對安全性要求不是很高的情況下使用UDP協議。發送(客戶請求發送數據)
很簡單,僅需QUdpSocket 的writeDatagram函數即可
[cpp]?view plaincopy
接收(服務器端監聽)
使用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 plaincopy
[cpp]?view plaincopy
[cpp]?view plaincopy
用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過濾一下)
雙擊進入詳細數據
可見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換了一下
分享到:?
?
轉載于:https://www.cnblogs.com/MingZznet/articles/3211004.html
總結
以上是生活随笔為你收集整理的四十六、Qt网络(六)UDP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mfs教程(四)
- 下一篇: C++:new 和 delete