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 = ,Qt::WindowFlags f= );
~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 = ;
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();
isStarted = true;
}
else
{
StartBtn->setText(tr("BEGIN"));
isStarted = false;
timer->stop();
}
}
void UdpServer::timeout()
{
QString msg = TextLineEdit->text();
int length=;
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 = );
~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 =; 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架构广播通信的更多相关文章

  1. QT网络编程Tcp下C/S架构的即时通信

    先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面. #ifndef TCPCLIENT_H #define TCPCLIENT_H #include <QDialog> #in ...

  2. c/c++ 网络编程 UDP 用if_nameindex和ioctl取得主机网络信息

    网络编程 UDP 用if_nameindex和ioctl取得主机网络信息 getifaddrs函数取得的东西太多了,如果只想取得网卡名字和网卡编号可以用下面的2个函数. 1,if_nameindex ...

  3. Qt网络编程QTcpServer和QTcpSocket的理解

    前一段时间通过调试Qt源码,大致了解了Qt的事件机制.信号槽机制.毕竟能力和时间有限.有些地方理解的并不是很清楚. 开发环境:Linux((fedora 17),Qt版本(qt-everywhere- ...

  4. c/c++ 网络编程 UDP 设定MTU

    网络编程 UDP 设定MTU MTU(Maximun Transmisson Unit):一次送信的最大size. 在程序里动态改变MTU.注意:程序运行需要root权限. 程序运行的方法: sudo ...

  5. c/c++ 网络编程 UDP up/down 网卡

    网络编程 UDP up/down 网卡 在程序里动态改变网卡的状态.注意:程序运行需要root权限. 程序运行的方法: sudo ./a.out 1,关闭网卡 #include <stdio.h ...

  6. c/c++ 网络编程 UDP 改变网关和网卡名字

    网络编程 UDP 改变网关和网卡名字 在程序里动态改变网关和网卡名字 1,改变网卡名字 #include <stdio.h> #include <string.h> #incl ...

  7. c/c++ 网络编程 UDP 改变网卡的硬件地址

    网络编程 UDP 改变网卡的硬件地址 在程序里动态改变网卡的硬件地址 1,取得网卡的硬件地址 #include <stdio.h> #include <string.h> #i ...

  8. c/c++ 网络编程 UDP 改变IP地址

    网络编程 UDP 改变IP地址 在程序里动态改变主机的IP地址 1,改变ipv4的地址 #include <stdio.h> #include <string.h> #incl ...

  9. c/c++ 网络编程 UDP 主机网络信息取得

    网络编程 UDP 主机网络信息取得 1,if_nametoindex 通过网卡名字取得网卡编号 2,if_indextoname 通过网卡编号取得网卡名字 #include <stdio.h&g ...

随机推荐

  1. Unity QualitySettings.antiAliasing 抗锯齿

    QualitySettings.antiAliasing 抗锯齿 Description 描述 Set The AA Filtering option. 设置AA过滤选项. The AntiAliaz ...

  2. 2016424王启元 Exp2 后门原理与实践

    一.实验准备 1.在实验前关闭或退出了防火墙.360杀毒软件.电脑卫士等所有的电脑保护软件,避免在实验过程中攻击时被拒绝.       2.使用Windows获linux shell (1)在Wind ...

  3. 聊聊Python ctypes 模块(转载)

    https://zhuanlan.zhihu.com/p/20152309?columnSlug=python-dev 作者:Jerry Jho链接:https://zhuanlan.zhihu.co ...

  4. Robot Framework_Ride(Settings)

    Settings 不管是测试套件还是测试用例都会有一个“Settings>>”的按钮,因为它默认是被折叠起来的,所以,一般不太容易发现它,更不知道点击它之后是可以展开的 1.测试用例的 S ...

  5. 常见Http协议响应码

    总体总结: 1XX:信息相应类,表示接受到请求并且继续处理 2XX:处理成功响应类,表示动作被成功的接收.理解和接受 3XX:重定向响应类,为了完成指定的动作,必须完成进一步处理和操作 4XX:客户端 ...

  6. linux mint 18.1 安装nvidia显卡驱动

    原文地址 http://www.gamersonlinux.com/forum/threads/updating-nvidia-drivers-mint.1746/ 主要步骤很简答 就是将ppa仓库地 ...

  7. location.false(true)

    location.reload()意思是从服务器端重新载入页面 ; location.false(true)意思是从缓存中重新载入.

  8. .netCore2.0 WebApi 传递form表单

    随着it的技术发展,目前越来越多的项目采用前后端分离的开发模式,通过webapi提供接口数据来进行交互 最近项目用的是.netCore WebApi,在最近的项目使用中发现一些问题,进行记录.个人简介 ...

  9. AJAX控件——多层弹出Accordion

    <asp:Accordion ID="Accordion1" runat="server" ContentCssClass="content&q ...

  10. [android] 切换界面的问题

    1. 界面重复创建的问题 2. 中间容器每次切换,都会清空容器中的子对象问题 3. 点击返回键的处理 解决重复创建的问题: 传递Class字节码对象,利用泛型来规定对象 判断界面是否存在,如果存在重复 ...