首页 > 其他 > 详细

Qt点滴:UDP协议的简易广播实例

时间:2014-06-04 18:11:16      阅读:797      评论:0      收藏:0      [点我收藏+]

Qt5实现的简易UDP广播程序,学习Qt 下UDP协议的基本使用。

创建两个工程,命名UDPclient和UDPserver.

又server发送广播,client负责接收。

 

------------

创建UDPserver时,选择dialog窗口类。

并用Qt设计器创建界面。

 

bubuko.com,布布扣

textedit用来输入广播的消息。

start按钮开始广播。

 

在.pro工程文档加入:

QT       += network

 

dialog.h中,包含头文件:

#include <QUdpSocket>
#include <QTimer>

及槽函数:

public slots:
    void StartBtnClicked();
    void TimeOut();

 

 

声明变量:

private:
    int port ;
    bool isStarted;
    QUdpSocket *udpSocket;
    QTimer *timer;

 

dialog.cpp中:

包含头文件:

#include <QHostAddress>

 

在构造函数中添加:

bubuko.com,布布扣
    this->setWindowTitle("UDP Server");

    this->timer = new QTimer();
    this->udpSocket = new QUdpSocket;
    this->isStarted = false;
    this->port = 45454;

    connect(this->timer,SIGNAL(timeout()),this,SLOT(TimeOut()));
    connect(this->ui->StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));
bubuko.com,布布扣

连接信号槽;

 

实现槽函数:

bubuko.com,布布扣
void Dialog::StartBtnClicked()
{
    if(!this->isStarted)
    {
        this->isStarted = true;
        this->ui->StartBtn->setText(tr("stop"));
        this->timer->start(1000);
    }
    else
    {
        this->isStarted = false;
        this->ui->StartBtn->setText(tr("start"));
        this->timer->stop();
    }
}

void Dialog::TimeOut()
{
    QString msg = ui->TextLineEdit->text();
    int length = 0;
    if(msg == "")
    {
        return;
    }
    if((length = this->udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port)) !=msg.length())
    {
        return;
    }
}
bubuko.com,布布扣

这样,点击发送按钮后触发StartBtnDClicked()函数,并初始化定时器为1000ms,即每隔一秒触发TimeOut()函数发送广播;

发送过程中再次点击按钮,发送停止;

 

其中,发送广播:writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port),QHostAddress::Broadcast获取地址,port定义1024至65536之间任意值即可;

 

bubuko.com,布布扣

 

-----------

然后是client,接收端;

在.pro工程文档加入:

QT       += network

 

 创建界面:

bubuko.com,布布扣

textEdit用来显示接收到的广播;

 

dialog.h中,包含头文件:

#include <QUdpSocket>

槽函数:

public slots:
    void CloseBtnClicked();
    void processPendingDatagram();

声明:

private:
    int port;
    QUdpSocket *UdpSocket;

 

dialg.cpp中:

 

包含

#include <QHostAddress>

构造函数中添加:

bubuko.com,布布扣
    this->setWindowTitle("UDP client");

    this->UdpSocket = new QUdpSocket;
    this->port = 45454;

    this->UdpSocket->bind(port,QUdpSocket::ShareAddress);

    connect(this->ui->CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked()));
    connect(this->UdpSocket,SIGNAL(readyRead()),this,SLOT(processPendingDatagram()));
bubuko.com,布布扣

 

实现槽函数:

bubuko.com,布布扣
void Dialog::CloseBtnClicked()
{
    this->close();
}

void Dialog::processPendingDatagram()
{
    while(this->UdpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(this->UdpSocket->pendingDatagramSize());
        this->UdpSocket->readDatagram(datagram.data(),datagram.size());
        this->ui->ReceiveTextEdit->insertPlainText(datagram);
    }
}
bubuko.com,布布扣

当有广播时,将收到的消息显示在文本编辑框中;

bubuko.com,布布扣

 

---------

 

运行效果:

 

打开一个client和一个server:

bubuko.com,布布扣bubuko.com,布布扣

 

输入 “ Hello world!  " 测试;

 

bubuko.com,布布扣bubuko.com,布布扣

 

Moran @ 2014.6.2

 

Qt点滴:UDP协议的简易广播实例,布布扣,bubuko.com

Qt点滴:UDP协议的简易广播实例

原文:http://www.cnblogs.com/moranBlogs/p/3764562.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!