你好,游客 登录 注册 搜索
背景:
阅读新闻

Qt之QUdpSocket网络广播

[日期:2012-05-29] 来源:Linux社区  作者:wyz365889 [字体:大 中 小]

实现代码如下:

  1. #include "UdpBroadcast.h"   
  2. #include "ui_UdpBroadcast.h"   
  3.   
  4. UdpBroadcast::UdpBroadcast(QWidget *parent) :  
  5.         QMainWindow(parent),  
  6.         ui(new Ui::UdpBroadcast)  
  7. {  
  8.     ui->setupUi(this);      
  9.       
  10.     ui->spinBox_time->setValue(1);  
  11.       
  12.     QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));  
  13.     //    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf-8"));   
  14.     //    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GBK"));   
  15.       
  16.     this->connect(ui->pushButton_BroadCast,SIGNAL(clicked()),this,SLOT(slotBroadcast()));  
  17.     this->connect(ui->pushButton_connt,SIGNAL(clicked()),this,SLOT(slotClientConnect()));  
  18.     this->connect(ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(slotTabChange(int)));  
  19. }  
  20.   
  21. UdpBroadcast::~UdpBroadcast()  
  22. {  
  23.     delete ui;  
  24. }  
  25.   
  26. void UdpBroadcast::slotTabChange(int i)  
  27. {  
  28.     if(i==0)  
  29.     {  
  30.         this->setWindowTitle(tr("服务端"));  
  31.     }  
  32.     else if(i==1)  
  33.     {  
  34.         this->setWindowTitle(tr("客户端"));  
  35.     }  
  36. }  
  37.   
  38. void UdpBroadcast::slotBroadcast()  
  39. {   
  40.       
  41.     m_Server_udp = new QUdpSocket(this);  
  42.       
  43.     m_timer = new QTimer(this);  
  44.       
  45.     this->connect(m_timer,SIGNAL(timeout()),this,SLOT(soltTiming()));  
  46.       
  47.     m_timer->start(ui->spinBox_time->text().toInt()*1000);      //定时器   
  48. }  
  49.   
  50. void UdpBroadcast::soltTiming()  
  51. {  
  52.     QString qStrMesg = ui->lineEdit_Mesg->text();  
  53.     qDebug()<<endl;  
  54.     qDebug()<<qStrMesg;  
  55.     if(qStrMesg == "")  
  56.     {  
  57.         return;  
  58.     }  
  59.       
  60.     int length;  
  61.     //发送   
  62.     if((length = m_Server_udp->writeDatagram(qStrMesg.toUtf8(),qStrMesg.toUtf8().length(),QHostAddress::Broadcast,55555)))  
  63.     {  
  64.         return;  
  65.     }  
  66. }  
  67.   
  68.   
  69.   
  70. void UdpBroadcast::slotClientConnect()  
  71. {  
  72.     ui->textEdit_Mesg->clear();  
  73.       
  74.     m_Client_udp = new QUdpSocket(this);  
  75.       
  76.     m_Client_udp->bind(55555);        
  77.       
  78.     this->connect(m_Client_udp,SIGNAL(readyRead()),this,SLOT(slotClientReadMesg()));  
  79.       
  80. }  
  81.   
  82.   
  83. void UdpBroadcast::slotClientReadMesg()  
  84. {  
  85.       
  86.     while(m_Client_udp->hasPendingDatagrams())  //是否有数据报过来   
  87.     {  
  88.         QByteArray qdata;  
  89.         qdata.resize(m_Client_udp->pendingDatagramSize());      //数据报大小   
  90.         m_Client_udp->readDatagram(qdata.data(),qdata.size());      //读取   
  91.         QString ss;  
  92.         ss = QString::fromUtf8(qdata);      //中文   
  93.         ui->textEdit_Mesg->append(ss);  
  94.     }  
  95.       
  96. }  
linux
相关资讯       QT教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款