qt4 udp QUdpSocket -------wireshark

来源:互联网 发布:pdfobject.js 兼容ie 编辑:程序博客网 时间:2024/04/26 11:52

四十六、Qt网络(六)UDP

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

发送(客户请求发送数据)
很简单,仅需QUdpSocket 的writeDatagram函数即可
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;}

接收(服务器端监听)
使用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.


private:    QUdpSocket *receiver;private slots:    void processPendingDatagram();

    receiver = new QUdpSocket(this);    receiver->bind(45454,QUdpSocket::ShareAddress);    connect(receiver,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));

void Widget::processPendingDatagram() //处理等待的数据报{    while(receiver->hasPendingDatagrams())  //拥有等待的数据报    {        QByteArray datagram; //拥于存放接收的数据报        datagram.resize(receiver->pendingDatagramSize());        //让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据        receiver->readDatagram(datagram.data(),datagram.size());        //接收数据报,将其存放到datagram中        ui->label->setText(datagram);        //将数据报内容显示出来    }}



用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换了一下

原创粉丝点击