qt在windows下的udp通信(最简单)
qt编程:windows下的udp通信
本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
环境:
主机:win7
开发环境:qt
功能:
用udp进行收发通信
界面:
源代码:
LssHost.pro:
- #-------------------------------------------------
- #
- # Project created by QtCreator 2013-09-22T09:36:44
- #
- #-------------------------------------------------
- QT += core gui
- QT += network
- TARGET = LssHost
- TEMPLATE = app
- SOURCES += main.cpp\
- mainwindow.cpp
- HEADERS += mainwindow.h
- FORMS += mainwindow.ui
mainwindows.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QtNetwork/QUdpSocket>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private:
- Ui::MainWindow *ui;
- QUdpSocket *udp_socket_tx;
- QUdpSocket *udp_socket_rx;
- QHostAddress Ip_Tx;
- int Port_Tx;
- private slots:
- void on_btn_cfg_clicked();
- void on_btn_tx_clicked();
- void rx_udp();
- };
- #endif // MAINWINDOW_H
mainwindows.cpp:
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- udp_socket_tx = new QUdpSocket(this);
- udp_socket_rx = new QUdpSocket(this);
- ui->btn_tx->setEnabled(false);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- //接收udp数据
- void MainWindow::rx_udp()
- {
- qDebug() << "rx";
- while (udp_socket_rx->hasPendingDatagrams())
- {
- QByteArray datagram;
- datagram.resize(udp_socket_rx->pendingDatagramSize());
- QHostAddress sender;
- quint16 senderPort;
- udp_socket_rx->readDatagram(datagram.data(), datagram.size(),
- &sender, &senderPort);
- ui->txt_rx->append(datagram);
- }
- }
- //发送按键
- void MainWindow::on_btn_tx_clicked()
- {
- QByteArray datagram = ui->txt_tx->toPlainText().toAscii();
- udp_socket_tx->writeDatagram(datagram, datagram.size(), Ip_Tx, Port_Tx);
- }
- //配置按键
- void MainWindow::on_btn_cfg_clicked()
- {
- bool ok;
- int port_rx = 0;
- //获得发送IP和端口
- Ip_Tx = QHostAddress(ui->txt_ip->text());
- Port_Tx = ui->txt_port_tx->text().toInt(&ok);
- //获得接收端口
- port_rx = ui->txt_port_rx->text().toInt(&ok);
- udp_socket_rx->bind(QHostAddress::Any, port_rx);
- //绑定接收信号槽
- connect(udp_socket_rx, SIGNAL(readyRead()),this, SLOT(rx_udp()));
- ui->btn_tx->setEnabled(true);
- }
main.cpp:
- #include <QtGui/QApplication>
- #include "mainwindow.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- MainWindow w;
- w.show();
- return a.exec();
- }
http://blog.csdn.net/jdh99/article/details/11892725
qt在windows下的udp通信(最简单)的更多相关文章
- QT在windows下的安装与配置
先了解Qt: Qt一直以来,分为商业.开源两个版本,商业版本为用户提供了二级制的动态库,直接安装既可以使用,但是需要花钱购买license,而开源版本则遵守GPL协议,提供了源码,用户需要自行编译,才 ...
- Windows下Anaconda的安装和简单使用
Windows下Anaconda的安装和简单使用 Anaconda is a completely free Python distribution (including for commercial ...
- Qt在Windows下的三种编程环境搭建
尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/17363165 从QT官网可以得知其支持的平台.编译器 ...
- qt在windows下编译遇到的一些问题
软件是在linux上写的,然而搬到windows上来遇到了好多问题.... 想跪.... 首先就是压根编译不了的问题....这个问题困扰我好久了....一直报错undefined reference ...
- Qt在Windows下的三种编程环境搭建(图文并茂,非常清楚)good
尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/17363165 从QT官网可以得知其支持的平台.编译器 ...
- 【Qt开发】Qt在Windows下的三种编程环境搭建
从QT官网可以得知其支持的平台.编译器和调试器的信息如图所示: http://qt-project.org/doc/qtcreator-3.0/creator-debugger-engines.htm ...
- 【QT开发】QT在windows下的exe应用程序如何在别人的电脑上直接运行
当你利用QT编译了一个可执行程序,需要将这个可执行程序拷贝到别人的电脑上运行,这个时候除了这个可执行程序外,还需要支持的库才可用运行.一般来说通过下面的方法可以实现. 首先,需要看你用的是什 ...
- windows下curl的安装和简单使用
curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP. 一 ...
- java 网络编程之UDP通信和简单的群聊程序
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
随机推荐
- Power aware dynamic scheduling in multiprocessor system employing voltage islands
Minimizing the overall power conservation in a symmetric multiprocessor system disposed in a system- ...
- Dll注入技术之消息钩子
转自:黑客反病毒 DLL注入技术之消息钩子注入 消息钩子注入原理是利用Windows 系统中SetWindowsHookEx()这个API,他可以拦截目标进程的消息到指定的DLL中导出的函数,利用这个 ...
- android studio的模拟器waiting for target device to come online原因
android studio的模拟器一直waiting for target device to come online,demo也运行不上去 如图所示: 你很可能运行的android 6.0 (AP ...
- Apache2.4.25 VirtualHost rewrite_module
LoadModule rewrite_module libexec/apache2/mod_rewrite.so Include /private/etc/apache2/extra/httpd-vh ...
- #上海ORACLE用户组2014在论坛#时刻
#上海ORACLE用户组2014年高峰论坛#精彩瞬间 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXNrbWFjbGVhbg==/font/5a6L5L ...
- PHP设计模式——迭代模式
声明:这一系列的博客引用<大话设计模式>.程洁作者. 迭代器模式:迭代器模式是遍历集合的成熟模式.迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对 ...
- QT5.5.1 为Qtcreator 编译的程序添加管理员权限
QT版本:5.5.1 QT Creator QT Creator 编译出来的程默认是不带管理员权限的.有时是需要管理员权限. 第一步: 创建文件 uac.manifest 添加如下代码 <?xm ...
- std::string 简单入门
string的定义原型 typedef basic_string<char, char_traits<char>, allocator<char> > string ...
- WPF 线程:使用调度程序构建反应速度更快的应用程序
原文:WPF 线程:使用调度程序构建反应速度更快的应用程序 作者:Shawn Wildermuth 原文:http://msdn.microsoft.com/msdnmag/issues/07/10/ ...
- wpf 屏蔽热键
原文:wpf 屏蔽热键 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a771948524/article/details/9428923 us ...