网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

QT网络编程UDP下C/S架构广播通信的示例分析-创新互联

今天就跟大家聊聊有关QT网络编程UDP下C/S架构广播通信的示例分析,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

为淮阳等地区用户提供了全套网页设计制作服务,及淮阳网站建设行业解决方案。主营业务为做网站、网站制作、淮阳网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

QT有封装好的UDP协议的类,QUdpSocket,里面有我们想要的函数接口。感兴趣的话,可以看看。

先搞服务端吧,写一个子类,继承QDialog类,起名为UdpServer类。头文件要引用我们上边说的QUdpSocket这个类,还有我们想要的布局的类。

#ifndef UDPSERVER_H
#define UDPSERVER_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
class UdpServer : public QDialog
{
 Q_OBJECT
public:
 UdpServer(QWidget *parent = 0,Qt::WindowFlags f= 0);
 ~UdpServer();
private:
 QLabel * TimerLabel;
 QLineEdit * TextLineEdit;
 QPushButton* StartBtn;
 QVBoxLayout * mainLayout;
 public slots:
 void StartBtnClicked();
 void timeout();
 private:
 int port;
 bool isStarted;
 QUdpSocket * udpSocket;
 QTimer *timer;
};
#endif // UDPSERVER_H

在.cpp文件里,我们先是把界面显示出来,然后用udp的writedategram把想要传的写进去。

#include "udpserver.h"


UdpServer::UdpServer(QWidget *parent,Qt::WindowFlags f)
 : QDialog(parent,f)
{
 setWindowTitle(tr("UDP SERVER"));
 TimerLabel = new QLabel(tr("show time:"),this);
 TextLineEdit = new QLineEdit(this);
 StartBtn = new QPushButton(tr("start"),this);

 mainLayout = new QVBoxLayout(this);
 mainLayout-> addWidget(TimerLabel);
 mainLayout-> addWidget(TextLineEdit);
 mainLayout-> addWidget(StartBtn);

 connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnClicked()));
 port = 5555;
 isStarted = false;
 udpSocket = new QUdpSocket(this);
 timer = new QTimer(this);
 connect(timer,SIGNAL(timeout()),this,SLOT(timeout()));

}

UdpServer::~UdpServer()
{

}
void UdpServer::StartBtnClicked()
{
 if(!isStarted)
 {
  StartBtn->setText(tr("STOP"));
  timer->start(1000);
  isStarted = true;
 }
 else
 {
  StartBtn->setText(tr("BEGIN"));
  isStarted = false;
  timer->stop();
 }
}
void UdpServer::timeout()
{
 QString msg = TextLineEdit->text();
 int length=0;
 if(msg=="")
 {
  return;
 }

 if((length=udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))!=msg.length())
 {
  qDebug() << msg.toLatin1();
  return;
 }
}

我这里用qDebug把要传的东西打印出来,进行测试,看看是否传过去了。

客户端:

#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include 
#include 
#include 
#include 
#include 
 class UdpClient : public QDialog
{
 Q_OBJECT
 public:
 UdpClient(QWidget *parent = 0);
 ~UdpClient();
 private:
 QTextEdit* ReceiceTextEdit;
 QPushButton* CloseBtn;
 QVBoxLayout* mainLayout;
 public slots:
 void CloseBtnClicked();
 void dataReceived();
 private:
 int port;
 QUdpSocket* udpSocket;
};
#endif // UDPCLIENT_H

客户端很简单,怎么实现布局,我就不多说了,主要是dataReceive函数。

#include "udpclient.h"
#include 
#include 


UdpClient::UdpClient(QWidget *parent)
 :QDialog(parent)
{
 setWindowTitle("UDP CLIENT");

 ReceiceTextEdit = new QTextEdit(this);
 CloseBtn = new QPushButton(tr("Close"),this);

 mainLayout = new QVBoxLayout(this);
 mainLayout->addWidget(ReceiceTextEdit);
 mainLayout->addWidget(CloseBtn);

 connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnClicked()));

 port =5555;

 udpSocket = new QUdpSocket(this);

 bool result = udpSocket->bind(port);

 if(!result)
 {
  QMessageBox::information(this,tr("ERROR"),tr("connect error"));
  return;
 }
 connect(udpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));

}
 UdpClient:: ~UdpClient()
{


}
void UdpClient::CloseBtnClicked()
{
 close();
}
void UdpClient::dataReceived()
{
 while(udpSocket->hasPendingDatagrams())
 {

  QByteArray datagram;
  datagram.resize(udpSocket->pendingDatagramSize());
  udpSocket->readDatagram(datagram.data(),datagram.size());
  QString msg=datagram.data();
  ReceiceTextEdit->insertPlainText(msg);

 }
}

最后显示一下界面,服务端发送hello。

QT网络编程UDP下C/S架构广播通信的示例分析

客户端收到的:

QT网络编程UDP下C/S架构广播通信的示例分析

不停的在打印hello。直到点击关闭,或者服务端停止。

看完上述内容,你们对QT网络编程UDP下C/S架构广播通信的示例分析有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联网站建设公司行业资讯频道,感谢大家的支持。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:QT网络编程UDP下C/S架构广播通信的示例分析-创新互联
网站路径:http://bjjierui.cn/article/pgghi.html

其他资讯