qt中的udp编程
UDP
QUdpSocket ---> upd socket
1.创建
QUdpSocket *p = new QUdpSocket();
2.最先接收数据一方 调用bind-> ip/port
bool QAbstractSocket::bind(const QHostAddress & address, quint16 port = 0, BindMode mode = DefaultForPlatform)
p->bind();
connect( ,SIGNAL(readyRead()),,SLOT(r));
3.接收数据
当 QUdpSocket 对象收到别一方发来的数据会发出信号 readyRead()
qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
r()
{
p->readDatagram();
}
4.发数据
qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)
p->writeDatagram();
recv.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QHostAddress>
#include<QUdpSocket>
#include<QByteArray>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void getmsg();
void on_pushButton_2_clicked(); private:
Ui::Dialog *ui;
QUdpSocket *up;
QHostAddress *host;
quint16 port,port1;
}; #endif // DIALOG_H
recv.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this); up = new QUdpSocket(this);
port = ; } Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{
up->bind(QHostAddress("192.168.1.30"),);
connect(up,SIGNAL(readyRead()),this,SLOT(getmsg()));
}
void Dialog::getmsg()
{//qDebug()<<host<<port;
QByteArray buf;
buf.resize(up->pendingDatagramSize());
QHostAddress host1;
// quint16 port1;
//qDebug()<<host<<port;
up->readDatagram(buf.data(),buf.size(),&host1,&port1);
// qDebug()<<buf.data();
QString tmp(buf.data());
ui->listWidget->addItem(tmp);
} void Dialog::on_pushButton_2_clicked()
{//qDebug()<<host<<port;
host = new QHostAddress("192.168.1.30"); QString tmp = ui->lineEdit->text();
qDebug()<<tmp; up->writeDatagram(tmp.toLatin1(),tmp.size(),*host,port1);
//qDebug()<<host<<port; }
ui
/********************************************************************************
** Form generated from reading UI file 'dialog.ui'
**
** Created by: Qt User Interface Compiler version 5.5.0
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/ #ifndef UI_DIALOG_H
#define UI_DIALOG_H #include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QListWidget>
#include <QtWidgets/QPushButton> QT_BEGIN_NAMESPACE class Ui_Dialog
{
public:
QPushButton *pushButton;
QPushButton *pushButton_2;
QListWidget *listWidget;
QLabel *label;
QLineEdit *lineEdit; void setupUi(QDialog *Dialog)
{
if (Dialog->objectName().isEmpty())
Dialog->setObjectName(QStringLiteral("Dialog"));
Dialog->resize(, );
pushButton = new QPushButton(Dialog);
pushButton->setObjectName(QStringLiteral("pushButton"));
pushButton->setGeometry(QRect(, , , ));
pushButton_2 = new QPushButton(Dialog);
pushButton_2->setObjectName(QStringLiteral("pushButton_2"));
pushButton_2->setGeometry(QRect(, , , ));
listWidget = new QListWidget(Dialog);
listWidget->setObjectName(QStringLiteral("listWidget"));
listWidget->setGeometry(QRect(, , , ));
label = new QLabel(Dialog);
label->setObjectName(QStringLiteral("label"));
label->setGeometry(QRect(, , , ));
lineEdit = new QLineEdit(Dialog);
lineEdit->setObjectName(QStringLiteral("lineEdit"));
lineEdit->setGeometry(QRect(, , , )); retranslateUi(Dialog); QMetaObject::connectSlotsByName(Dialog);
} // setupUi void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", ));
pushButton->setText(QApplication::translate("Dialog", "bind", ));
pushButton_2->setText(QApplication::translate("Dialog", "send", ));
label->setText(QApplication::translate("Dialog", "recv", ));
} // retranslateUi }; namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui QT_END_NAMESPACE #endif // UI_DIALOG_H
send.h
#ifndef DIALOG_H
#define DIALOG_H #include <QDialog>
#include<QDebug>
#include<QUdpSocket>
#include<QHostAddress>
#include<QByteArray>
namespace Ui {
class Dialog;
} class Dialog : public QDialog
{
Q_OBJECT public:
explicit Dialog(QWidget *parent = );
~Dialog(); private slots:
void on_pushButton_clicked();
void getmsg();
private:
Ui::Dialog *ui;
QUdpSocket *up;
QHostAddress *host,host1;
quint16 port,port1;
}; #endif // DIALOG_H
send.cpp
#include "dialog.h"
#include "ui_dialog.h" Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
// up = new QUdpSocket;
port = ;
host = new QHostAddress("192.168.1.30");
up = new QUdpSocket();
} Dialog::~Dialog()
{
delete ui;
} void Dialog::on_pushButton_clicked()
{ QString tmp = ui->lineEdit->text();
qDebug()<<tmp;
up->writeDatagram(tmp.toLatin1(),*host,port);
connect(up,SIGNAL(readyRead()),this,SLOT(getmsg()));
}
void Dialog::getmsg()
{
QByteArray buf;
buf.resize(up->pendingDatagramSize());
QHostAddress host1;
quint16 port1;
up->readDatagram(buf.data(),buf.size(),&host1,&port1);
//qDebug()<<buf.data();
QString tmp(buf.data());
ui->listWidget->addItem(tmp);
}
qt中的udp编程的更多相关文章
- QT中的SOCKET编程(QT-2.3.2)
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- QT中的SOCKET编程
转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/ QT中的SOCKET编程 2008-10-07 23:13 ...
- Qt中的多线程编程
http://www.ibm.com/developerworks/cn/linux/l-qt-mthrd/ Qt 作为一种基于 C++ 的跨平台 GUI 系统,能够提供给用户构造图形用户界面的强大功 ...
- Qt中的串口编程之三
QtSerialPort 今天我们来介绍一下QtSerialPort模块的源代码,学习一下该可移植的串口编程库是怎么实现的. 首先,我们下载好了源代码之后,使用QtCreator打开整个工程,可以看到 ...
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
1 新建一个项目:TCPServer.pro A 修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...
- Qt下的udp编程
项目需要一个基于udp的客户端, 看着Qt是有个QUdpSocket的类的, 但自带的例子和类的说明都没咋说明白: 怎么用一个QUdpSocket既当发送端, 又当接收端? 谷歌一顿后, 发现很多老内 ...
- Qt中的串口编程之一
QtSerialPort 简介 功能介绍 SerialPort SerialPortInfo 源代码 编译和安装 配置编译环境 Perl只是在Qt5的时候才需要Qt4的情况下可以不配置 使用如下推荐步 ...
- golang中的udp编程
1. udp server package main import ( "fmt" "net" ) func main() { // udp server li ...
- qt中的tcp编程
server .server.h #define DIALOG_H #include <QDialog> #include <QTcpServer> #include < ...
随机推荐
- HDU 1232 畅通工程(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...
- TP框架自带的正则验证的规则
thinkphp框架里面自带有很多自动验证的规则,下面是框架自带的正则验证的规则,官方的说明文档里面没有这么多,所以记下来,以备使用. view sourceprint? 01 static $reg ...
- PHP面试题:HTTP中POST、GET、PUT、DELETE方式的区别
HTTP定义了与服务器交互的不同的方法,最基本的是POST.GET.PUT.DELETE,与其比不可少的URL的全称是资源描述符,我们可以这样理解:url描述了一个网络上资源,而post.get.pu ...
- MyEclipse或Eclipse导出JavaDoc中文乱码问题解决
- xml报错 Parse Fatal Error :在实体引用中,实体名称必须紧跟在'&'后面
修改jndi配置文件中的密码后,重启tomcat报错如下 实际问题是xml中默认’&’是非法字符,用 & 替代
- sp_getAppLock使用
sp_getAppLock 获取程序资源锁,简单的说就是调用此函数可以达到我们程序中.NET的lock锁的作用. 作用域是当前数据库下 四个参数: @resource(必填):资源名称,类型nvar ...
- struts2 添加请求后缀的3种方式
第一种方式在struts.xml文件中添加 <constant name="struts.action.extension" value="">&l ...
- input标签(待填坑)
input标签几种属性值 button:用作定义按钮 checkbox:定义复选框 file:供文件上传 hidden:定义隐藏的输入字段 image:图像形式的提交按钮 password:密码字段 ...
- shell第三篇
第三篇本文摘自鸟哥的私房菜:http://cn.linux.vbird.org/linux_basic/0105computers.php#program(当年看的时候浮光掠影,现在回头发现,经典就是 ...
- python-day2数据类型
内容介绍 数据类型 字符编码 文件处理 1.什么是数据? x=10 , 10是我们要存储的数据. 2.为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3.数据类 ...