码迷,mamicode.com
首页 > Windows程序 > 详细

qt在windows下的udp通信(最简单)

时间:2016-12-10 00:51:07      阅读:582      评论:0      收藏:0      [点我收藏+]

标签:最简   size   roo   bar   pac   tag   exe   通信   copyto   

qt编程:windows下的udp通信

 

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

 

环境:

主机:win7

开发环境:qt

 

功能:

用udp进行收发通信

 

界面:

技术分享

 

源代码:

LssHost.pro:

 

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


mainwindows.h

 

 

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


mainwindows.cpp:

 

 

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


main.cpp:

 

 

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

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

qt在windows下的udp通信(最简单)

标签:最简   size   roo   bar   pac   tag   exe   通信   copyto   

原文地址:http://www.cnblogs.com/findumars/p/6152840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!