qt在windows下的udp通訊(最簡單)

來源:互聯網
上載者:User

標籤:最簡   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通訊(最簡單)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.