日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

qt編程:windows下的udp通信

系統(tǒng) 3218 0

qt編程:windows下的udp通信


本文博客鏈接: http://blog.csdn.net/jdh99 ,作者:jdh,轉(zhuǎn)載請(qǐng)注明.


環(huán)境:

主機(jī):win7

開發(fā)環(huán)境:qt


功能:

用udp進(jìn)行收發(fā)通信


界面:

qt編程:windows下的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數(shù)據(jù)
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);
         }
}

//發(fā)送按鍵
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;

    //獲得發(fā)送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);

    //綁定接收信號(hào)槽
    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();
}

  


qt編程:windows下的udp通信


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 东丰县| 曲水县| 油尖旺区| 简阳市| 云浮市| 沙湾县| 长春市| 井陉县| 嘉义市| 罗定市| 甘南县| 临潭县| 丹阳市| 克什克腾旗| 新竹市| 上犹县| 阿瓦提县| 黄梅县| 永年县| 会同县| 通道| 苗栗县| 仙居县| 玉环县| 武义县| 独山县| 武邑县| 潜山县| 兴国县| 阿城市| 绥化市| 安庆市| 西青区| 土默特左旗| 杭锦后旗| 饶阳县| 留坝县| 阳西县| 本溪市| 岳阳县| 九江市|