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 ...
随机推荐
- 在ubuntu下使用Eclipse搭建Hadoop开发环境
一.安装准备1.JDK版本:jdk1.7.0(jdk-7-linux-i586.tar.gz)2.hadoop版本:hadoop-1.1.1(hadoop-1.1.1.tar.gz)3.eclipse ...
- 【AAA】AAA协议介绍
AAA AAA简介 AAA是认证(Authentication).授权(Authorization)和计费(Accounting)的简称,是网络安全中进行访问控制的一种安全管理机制,提供认证.授权和计 ...
- Firebird 日期时间
查询当前时间: 1.使用内置系统变量 select current_timestamp from rdb$database 2.使用now字符串转换 select cast('NOW' as time ...
- Firebird execute block 批处理
火鸟的批处理,效率好高,使用简单. execute block as declare variable i ; begin ) do begin :i = :i + ; insert into m_u ...
- struts2随笔
1.struts.properties配置常量等同于struts.xml中配置(置于类加载路径下面)struts.multipart.maxSize文件上传最大大小struts.action.exte ...
- class path resource [logback.xml] cannot be resolved to URL because it does not exist 问题解决
今天在自己搭建Springboot 框架的时候,在配置 logging.config=classpath:logback.xml 出现找不到这个文件的错误 经发现是maven的一个写法问题,本来我是打 ...
- 13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 简述Spring及配置
简述Spring及配置 Spring最主要的思想就是IoC(Inversionof Control,控制反转),或者成为DI(Dependency Injection,依赖注入) 一.springMV ...
- C Primer Plus(第六版)中文版 中的错误1
#include<stdio.h> #include<stdlib.h> #include<string.h> #define TSIZE 45 struct fi ...
- Redis 常见命令
0. 5种数据类型 String(字符串) List(列表) Hash(字典) Set(集合) Sorted Set(有序集合) 1. String 字符串 set key value 设置key=v ...