qt编程:windows下的udp通信

本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.

环境:

主机:win7

开发环境:qt

功能:

用udp进行收发通信

界面:

源代码:

LssHost.pro:

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2013-09-22T09:36:44
  4. #
  5. #-------------------------------------------------
  6. QT       += core gui
  7. QT       += network
  8. TARGET = LssHost
  9. TEMPLATE = app
  10. SOURCES += main.cpp\
  11. mainwindow.cpp
  12. HEADERS  += mainwindow.h
  13. FORMS    += mainwindow.ui

mainwindows.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QtNetwork/QUdpSocket>
  5. namespace Ui {
  6. class MainWindow;
  7. }
  8. class MainWindow : public QMainWindow
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MainWindow(QWidget *parent = 0);
  13. ~MainWindow();
  14. private:
  15. Ui::MainWindow *ui;
  16. QUdpSocket *udp_socket_tx;
  17. QUdpSocket *udp_socket_rx;
  18. QHostAddress Ip_Tx;
  19. int Port_Tx;
  20. private slots:
  21. void on_btn_cfg_clicked();
  22. void on_btn_tx_clicked();
  23. void rx_udp();
  24. };
  25. #endif // MAINWINDOW_H

mainwindows.cpp:

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. MainWindow::MainWindow(QWidget *parent) :
  4. QMainWindow(parent),
  5. ui(new Ui::MainWindow)
  6. {
  7. ui->setupUi(this);
  8. udp_socket_tx = new QUdpSocket(this);
  9. udp_socket_rx = new QUdpSocket(this);
  10. ui->btn_tx->setEnabled(false);
  11. }
  12. MainWindow::~MainWindow()
  13. {
  14. delete ui;
  15. }
  16. //接收udp数据
  17. void MainWindow::rx_udp()
  18. {
  19. qDebug() << "rx";
  20. while (udp_socket_rx->hasPendingDatagrams())
  21. {
  22. QByteArray datagram;
  23. datagram.resize(udp_socket_rx->pendingDatagramSize());
  24. QHostAddress sender;
  25. quint16 senderPort;
  26. udp_socket_rx->readDatagram(datagram.data(), datagram.size(),
  27. &sender, &senderPort);
  28. ui->txt_rx->append(datagram);
  29. }
  30. }
  31. //发送按键
  32. void MainWindow::on_btn_tx_clicked()
  33. {
  34. QByteArray datagram = ui->txt_tx->toPlainText().toAscii();
  35. udp_socket_tx->writeDatagram(datagram, datagram.size(), Ip_Tx, Port_Tx);
  36. }
  37. //配置按键
  38. void MainWindow::on_btn_cfg_clicked()
  39. {
  40. bool ok;
  41. int port_rx = 0;
  42. //获得发送IP和端口
  43. Ip_Tx = QHostAddress(ui->txt_ip->text());
  44. Port_Tx = ui->txt_port_tx->text().toInt(&ok);
  45. //获得接收端口
  46. port_rx = ui->txt_port_rx->text().toInt(&ok);
  47. udp_socket_rx->bind(QHostAddress::Any, port_rx);
  48. //绑定接收信号槽
  49. connect(udp_socket_rx, SIGNAL(readyRead()),this, SLOT(rx_udp()));
  50. ui->btn_tx->setEnabled(true);
  51. }

main.cpp:

    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. MainWindow w;
    7. w.show();
    8. return a.exec();
    9. }

http://blog.csdn.net/jdh99/article/details/11892725

qt在windows下的udp通信(最简单)的更多相关文章

  1. QT在windows下的安装与配置

    先了解Qt: Qt一直以来,分为商业.开源两个版本,商业版本为用户提供了二级制的动态库,直接安装既可以使用,但是需要花钱购买license,而开源版本则遵守GPL协议,提供了源码,用户需要自行编译,才 ...

  2. Windows下Anaconda的安装和简单使用

    Windows下Anaconda的安装和简单使用 Anaconda is a completely free Python distribution (including for commercial ...

  3. Qt在Windows下的三种编程环境搭建

    尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/17363165 从QT官网可以得知其支持的平台.编译器 ...

  4. qt在windows下编译遇到的一些问题

    软件是在linux上写的,然而搬到windows上来遇到了好多问题.... 想跪.... 首先就是压根编译不了的问题....这个问题困扰我好久了....一直报错undefined reference ...

  5. Qt在Windows下的三种编程环境搭建(图文并茂,非常清楚)good

    尊重作者,支持原创,如需转载,请附上原地址:http://blog.csdn.net/libaineu2004/article/details/17363165 从QT官网可以得知其支持的平台.编译器 ...

  6. 【Qt开发】Qt在Windows下的三种编程环境搭建

    从QT官网可以得知其支持的平台.编译器和调试器的信息如图所示: http://qt-project.org/doc/qtcreator-3.0/creator-debugger-engines.htm ...

  7. 【QT开发】QT在windows下的exe应用程序如何在别人的电脑上直接运行

     当你利用QT编译了一个可执行程序,需要将这个可执行程序拷贝到别人的电脑上运行,这个时候除了这个可执行程序外,还需要支持的库才可用运行.一般来说通过下面的方法可以实现.     首先,需要看你用的是什 ...

  8. windows下curl的安装和简单使用

    curl是利用URL语法在命令行方式下工作的开源文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP. 一 ...

  9. java 网络编程之UDP通信和简单的群聊程序

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

随机推荐

  1. Power aware dynamic scheduling in multiprocessor system employing voltage islands

    Minimizing the overall power conservation in a symmetric multiprocessor system disposed in a system- ...

  2. Dll注入技术之消息钩子

    转自:黑客反病毒 DLL注入技术之消息钩子注入 消息钩子注入原理是利用Windows 系统中SetWindowsHookEx()这个API,他可以拦截目标进程的消息到指定的DLL中导出的函数,利用这个 ...

  3. android studio的模拟器waiting for target device to come online原因

    android studio的模拟器一直waiting for target device to come online,demo也运行不上去 如图所示: 你很可能运行的android 6.0 (AP ...

  4. Apache2.4.25 VirtualHost rewrite_module

    LoadModule rewrite_module libexec/apache2/mod_rewrite.so Include /private/etc/apache2/extra/httpd-vh ...

  5. #上海ORACLE用户组2014在论坛#时刻

    #上海ORACLE用户组2014年高峰论坛#精彩瞬间 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYXNrbWFjbGVhbg==/font/5a6L5L ...

  6. PHP设计模式——迭代模式

    声明:这一系列的博客引用<大话设计模式>.程洁作者. 迭代器模式:迭代器模式是遍历集合的成熟模式.迭代器模式的关键是将遍历集合的任务交给一个叫做迭代器的对象,它的工作时遍历并选择序列中的对 ...

  7. QT5.5.1 为Qtcreator 编译的程序添加管理员权限

    QT版本:5.5.1 QT Creator QT Creator 编译出来的程默认是不带管理员权限的.有时是需要管理员权限. 第一步: 创建文件 uac.manifest 添加如下代码 <?xm ...

  8. std::string 简单入门

    string的定义原型 typedef basic_string<char, char_traits<char>, allocator<char> > string ...

  9. WPF 线程:使用调度程序构建反应速度更快的应用程序

    原文:WPF 线程:使用调度程序构建反应速度更快的应用程序 作者:Shawn Wildermuth 原文:http://msdn.microsoft.com/msdnmag/issues/07/10/ ...

  10. wpf 屏蔽热键

    原文:wpf 屏蔽热键 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a771948524/article/details/9428923   us ...