Qt之QUdpSocket定时器网络广播

 

UDP(用户数据报协议)是一个轻量级的,不可靠的,面向数据报的,无连接协议。当可靠性不是很重要的时候就可以使用该协议。

 

程序实现功能:1.设定时间发送信息 2.服务端和客户端在同一个程序 3.可以发送中文 4.Tabwidget组件简单使用

 

困扰:在linux运行正常,可是在window在就出现一次发三条信息,无解中!

 

实现效果图:

Qt之QUdpSocket定时器网络广播_第1张图片

 

 

Qt之QUdpSocket定时器网络广播_第2张图片

头文件:

#ifndef UDPBROADCAST_H
#define UDPBROADCAST_H

#include <QMainWindow>
#include <QTimer>
#include <QDebug>
#include <QUdpSocket>
#include <QTextCodec>

namespace Ui {
    class UdpBroadcast;
}

class UdpBroadcast : public QMainWindow
{
    Q_OBJECT

public:
    explicit UdpBroadcast(QWidget *parent = 0);
    ~UdpBroadcast();

private:
    Ui::UdpBroadcast *ui;
    QTimer *m_timer;
     QUdpSocket *m_Server_udp;
     QUdpSocket *m_Client_udp;

private slots:
    void soltTiming();
    void slotBroadcast();

    void slotClientConnect();
    void slotClientReadMesg();
        void slotTabChange(int i);
};

#endif // UDPBROADCAST_H


 

实现代码如下:

#include "UdpBroadcast.h"
#include "ui_UdpBroadcast.h"

UdpBroadcast::UdpBroadcast(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::UdpBroadcast)
{
    ui->setupUi(this);    
    
    ui->spinBox_time->setValue(1);
    
    QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
    //    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));
    //    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));
    
    this->connect(ui->pushButton_BroadCast,SIGNAL(clicked()),this,SLOT(slotBroadcast()));
    this->connect(ui->pushButton_connt,SIGNAL(clicked()),this,SLOT(slotClientConnect()));
    this->connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(slotTabChange(int)));
}

UdpBroadcast::~UdpBroadcast()
{
    delete ui;
}

void UdpBroadcast::slotTabChange(int i)
{
    if(i==0)
    {
        this->setWindowTitle(tr("服务端"));
    }
    else if(i==1)
    {
        this->setWindowTitle(tr("客户端"));
    }
}

void UdpBroadcast::slotBroadcast()
{ 
    
    m_Server_udp = new QUdpSocket(this);
    
    m_timer = new QTimer(this);
    
    this->connect(m_timer,SIGNAL(timeout()),this,SLOT(soltTiming()));
    
    m_timer->start(ui->spinBox_time->text().toInt()*1000);      //定时器
}

void UdpBroadcast::soltTiming()
{
    QString qStrMesg = ui->lineEdit_Mesg->text();
    qDebug()<<endl;
    qDebug()<<qStrMesg;
    if(qStrMesg == "")
    {
        return;
    }
    
    int length;
    //发送
    if((length = m_Server_udp->writeDatagram(qStrMesg.toUtf8(),qStrMesg.toUtf8().length(),QHostAddress::Broadcast,55555)))
    {
        return;
    }
}



void UdpBroadcast::slotClientConnect()
{
    ui->textEdit_Mesg->clear();
    
    m_Client_udp = new QUdpSocket(this);
    
    m_Client_udp->bind(55555);      
    
    this->connect(m_Client_udp,SIGNAL(readyRead()),this,SLOT(slotClientReadMesg()));
    
}


void UdpBroadcast::slotClientReadMesg()
{
    
    while(m_Client_udp->hasPendingDatagrams())  //是否有数据报过来
    {
        QByteArray qdata;
        qdata.resize(m_Client_udp->pendingDatagramSize());      //数据报大小
        m_Client_udp->readDatagram(qdata.data(),qdata.size());      //读取
        QString ss;
        ss = QString::fromUtf8(qdata);      //中文
        ui->textEdit_Mesg->append(ss);
    }
    
}


 

 

 

 

 

你可能感兴趣的:(timer,UI,server,网络,qt,Signal)