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


    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

  • 相关阅读:
    嘉佣坊
    HTTPS
    OWIN 为WebAPI
    C#并行编程
    ASP.NET 运行
    DocFX
    oracle SELECT INTO 和 INSERT INTO SELECT 两种表复制语句详解
    Facebook新框架React Native,一套搞定App开发[转]
    MVC 中使用 SignalR 实现推送功能
    生产都消费者模式的一个demo,消费者设置缓存
  • 原文地址:https://www.cnblogs.com/findumars/p/6152840.html
Copyright © 2020-2023  润新知