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 < ...
随机推荐
- NGINX 配置404错误页面转向
什么是404页面 如果碰巧网站出了问题,或者用户试图访问一个并不存在的页面时,此时服务器会返回代码为404的错误信息,此时对应页面就是404页面.404页面的默认内容和具体的服务器有关.如果后台用的是 ...
- 栏目class导航
<div id="index_nav"> <div class="index_nav"> <ul> <!-- 调用栏目 ...
- SQLITE3 使用总结(3~5)(转)
3 不使用回调查询数据库/ `- ^# T6 ?, F: H* m2 ~# ~上 面介绍的 sqlite3_exec 是使用回调来执行 select 操作.还有一个方法可以直接查询而不需要回调.但是, ...
- 利用xcode生成的app生成可以在iphone和itouch上运行的ipa安装包
在编译好的真机版目录下的.app文件,至于生成真机可以运行的app的方法,有两种方式,一种是交99美元获得一个证书,另外一种是破解的方式,在此不再详述,本文假设你已经生成了真机上可以运行的app包了( ...
- 关于Serializable的serialVersionUID
在实现了Serializable接口的class中,需要声明一个long serialVersionUID,用来标明当前class的版本号,但很多人在编程时,总是不原意去声明这个serialVersi ...
- Django实现组合搜索
一.实现方法 1.纯模板语言实现 2.自定义simpletag实现(本质是简化了纯模板语言的判断) 二.基本原理 原理都是通过django路由系统,匹配url筛选条件,将筛选条件作为数据库查询结果,返 ...
- ListCtrl控件
一 CListCtrl类型 LVS_EDITLABELS LVS_OWNERDRAWFIXED LVS_REPORT LVS_SHOWSELALWAYS LVS_SINGLESEL LVS_SMALL ...
- DOM4J使用简介
Dom4j 使用简介 作者:冰云 icecloud(AT)sina.com 时间:2003.12.15 版权声明: 本文由冰云完成,首发于CSDN,未经许可,不得使用于任何商业用途. 文中代码部分 ...
- android Android SDK Manager遇到的问题
打开Android SDK Manager 1点击左上角的tools-->options:将Proxy Settings 里的HTTP Proxy Server和HTTP Proxy Port分 ...
- 李忠益TP5商城项目笔记(待完成)
商品种类的无限极分类 $data=db('goods_type')->field(['*','concat(path,",",id)'=>'paths'])->o ...