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 = ,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架构广播通信的更多相关文章
- QT网络编程Tcp下C/S架构的即时通信
先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面. #ifndef TCPCLIENT_H #define TCPCLIENT_H #include <QDialog> #in ...
- c/c++ 网络编程 UDP 用if_nameindex和ioctl取得主机网络信息
网络编程 UDP 用if_nameindex和ioctl取得主机网络信息 getifaddrs函数取得的东西太多了,如果只想取得网卡名字和网卡编号可以用下面的2个函数. 1,if_nameindex ...
- Qt网络编程QTcpServer和QTcpSocket的理解
前一段时间通过调试Qt源码,大致了解了Qt的事件机制.信号槽机制.毕竟能力和时间有限.有些地方理解的并不是很清楚. 开发环境:Linux((fedora 17),Qt版本(qt-everywhere- ...
- c/c++ 网络编程 UDP 设定MTU
网络编程 UDP 设定MTU MTU(Maximun Transmisson Unit):一次送信的最大size. 在程序里动态改变MTU.注意:程序运行需要root权限. 程序运行的方法: sudo ...
- c/c++ 网络编程 UDP up/down 网卡
网络编程 UDP up/down 网卡 在程序里动态改变网卡的状态.注意:程序运行需要root权限. 程序运行的方法: sudo ./a.out 1,关闭网卡 #include <stdio.h ...
- c/c++ 网络编程 UDP 改变网关和网卡名字
网络编程 UDP 改变网关和网卡名字 在程序里动态改变网关和网卡名字 1,改变网卡名字 #include <stdio.h> #include <string.h> #incl ...
- c/c++ 网络编程 UDP 改变网卡的硬件地址
网络编程 UDP 改变网卡的硬件地址 在程序里动态改变网卡的硬件地址 1,取得网卡的硬件地址 #include <stdio.h> #include <string.h> #i ...
- c/c++ 网络编程 UDP 改变IP地址
网络编程 UDP 改变IP地址 在程序里动态改变主机的IP地址 1,改变ipv4的地址 #include <stdio.h> #include <string.h> #incl ...
- c/c++ 网络编程 UDP 主机网络信息取得
网络编程 UDP 主机网络信息取得 1,if_nametoindex 通过网卡名字取得网卡编号 2,if_indextoname 通过网卡编号取得网卡名字 #include <stdio.h&g ...
随机推荐
- Unity5 2D Animation
1. 所有的动画保存在 .anim 后缀的文件里.2. Animation 标签用来编辑一堆 Animation clip,每一个clip是一个图片序列,也就是动图.动画的最小控制单位就是clip,一 ...
- 使用maven构建多模块项目_记录
参照孤傲苍狼的博客:https://www.cnblogs.com/xdp-gacl/p/4242221.html 备注:博客中的生成语句,适用于maven3.0.5以上,若为3.0.5以下,则将cr ...
- 读取obj文件用Mesh创建实例化
using UnityEngine; using System.Collections; using System.IO; using System.Collections.Generic; usin ...
- MySQL比较运算符的子查询
使用比较运算符的子查询 =.>.<.>=.<=.<>.!=.<=> 语法结构 operand comparison_operator subquery ...
- 这是关于FastJson的一个使用Demo,在Java环境下验证的
public class User { private int id; private String name; public int getId() { return id; } public vo ...
- 网易和淘宝的rem方案剖析
以下内容到分割线前是引用前端大牛的文章,方便大家理解博主内容): 从网易与淘宝的font-size思考前端设计稿与工作流 1. 简单问题简单解决 我觉得有些web app并一定很复杂,比如拉勾网,你看 ...
- Iterator和for...of循环
Iterator和for...of循环 Iterator(遍历器)的概念 数据结构的默认Iterator接口 调用Iterator接口的场合 字符串的Iterator接口 Iterator接口与Gen ...
- java中四种引用类型(对象的强、软、弱和虚引用)
对象的强.软.弱和虚引用在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK 1.2 ...
- wcf 基本配置
<system.serviceModel> <services> <service name="ServiceUpdater.ServiceUpdate&quo ...
- wxss无法调用本地资源图片
微信小程序中,wxss中不能调用本地资源图片作为背景图片,奇怪的是在微信开发者工具中可以调用,但是到了真机预览的时候发现并不行,有的电脑上的连微信开发者工具里也不可以调用. 原因在于小程序上传的是代码 ...