QT网络编程UDP下C/S架构广播通信(实例讲解)

 更新时间:2017年07月27日 09:28:12   投稿:jingxian  
下面小编就为大家带来一篇QT网络编程UDP下C/S架构广播通信(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

#ifndef UDPSERVER_H
#define UDPSERVER_H

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QtNetwork/QUdpSocket>
#include <QtNetwork/QHostAddress>
#include <QTimer>
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 <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QtNetwork/QUdpSocket>
 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 <QMessageBox>
#include <QHostAddress>


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。

客户端收到的:

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

以上这篇QT网络编程UDP下C/S架构广播通信(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 一篇文章带你了解C++语法基础--字符串

    一篇文章带你了解C++语法基础--字符串

    这篇文章主要介绍了C++常用字符串分割方法实例汇总,包括了strtok函数、STL、Boost等常用的各类字符串分割方法,非常具有实用价值,需要的朋友可以参考下
    2021-08-08
  • C++如何用数组模拟链表

    C++如何用数组模拟链表

    大家好,本篇文章主要讲的是C++如何用数组模拟链表,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C++使用JsonCpp库操作json格式数据示例

    C++使用JsonCpp库操作json格式数据示例

    这篇文章主要介绍了C++使用JsonCpp库操作json格式数据,结合实例形式详细分析了JsonCpp库的下载及C++使用JsonCpp库对json格式数据序列化相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • C++ 计数排序实例详解

    C++ 计数排序实例详解

    这篇文章主要介绍了C++ 计数排序实例详解的相关资料,需要的朋友可以参考下
    2017-07-07
  • C++ STL中一些常用算法总结

    C++ STL中一些常用算法总结

    都说STL是数据容器与算法的高度组合,在前面的文章中我们介绍了常见的几种容器,vector、list、map、deque等,今天我们再来介绍下STL中常用的一些算法,需要的朋友可以参考下
    2024-02-02
  • C语言实现循环链表

    C语言实现循环链表

    这篇文章主要为大家详细介绍了C语言实现循环链表,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • C++学习小结之数据类型及转换方式

    C++学习小结之数据类型及转换方式

    本文给大家分享的是本人在学习C++过程中的一个小心得,关于数据类型和转换方式的,这里记录下来,推荐给菜鸟们,高手大神请直接飘过。
    2015-07-07
  • C++中const char*、char const*、char * const三者的区别

    C++中const char*、char const*、char * const三者的区别

    这篇文章主要介绍了C++中const char*、char const*、char * const三者的区别,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • 浅析操作系统中的虚拟地址与物理地址

    浅析操作系统中的虚拟地址与物理地址

    本文主要介绍了操作系统中的虚拟地址与物理地址。在早期的计算机中,要运行一个程序,会把这些程序全都装入内存,程序都是直接运行在内存上的,也就是说程序中访问的内存地址都是实际的物理内存地址。那当程序同时运行多个程序时,操作系统是如何为这些程序分配内存的呢
    2021-06-06
  • 浅谈C++有理数的表达和计算

    浅谈C++有理数的表达和计算

    这篇文章主要为大家详细介绍了C++有理数的表达和计算,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11

最新评论