QUdpSocket进行网络传输
2014-02-25 20:41
316 查看
服务器端:
客户端:
结果:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QUdpSocket> #include <QMessageBox> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; private: QUdpSocket *UdpSocket; private slots: void RecvServerMSG(); void on_pushButton_clicked(); private: QHostAddress senderAddress; quint16 senderPort; }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); UdpSocket = new QUdpSocket(this); bool conn = UdpSocket->bind(QHostAddress::LocalHost,7755); if(!conn) { QMessageBox box; box.setText("链接错误 "); box.exec(); } else { connect(UdpSocket, SIGNAL(readyRead()), this, SLOT(RecvServerMSG())); } } Widget::~Widget() { delete ui; } void Widget::RecvServerMSG() { while(UdpSocket->hasPendingDatagrams()) //is read data { QByteArray data; data.resize(UdpSocket->pendingDatagramSize()); UdpSocket->readDatagram(data.data(), data.size(), &senderAddress, &senderPort); //read data QString strText = QString(data); ui->textEdit->insertPlainText(strText + "\r\n"); } } void Widget::on_pushButton_clicked() { QMessageBox box; QString strSendText = ui->textEdit->toPlainText(); QByteArray datagram = strSendText.toUtf8(); if(strSendText.length() == 0) { box.setText("Please Input text to send!"); } int retLen = UdpSocket->writeDatagram(datagram.data(),datagram.size(), senderAddress, senderPort); if(retLen) { box.setText("send success!"); } box.exec(); }
客户端:
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QUdpSocket> #include <QMessageBox> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); private: Ui::Widget *ui; private: QUdpSocket *UdpSocket; private slots: void SendToClient(); void RecvServerMSG(); }; #endif // WIDGET_H
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); UdpSocket = new QUdpSocket(this); connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(SendToClient())); connect(UdpSocket, SIGNAL(readyRead()), this, SLOT(RecvServerMSG())); } Widget::~Widget() { delete ui; } void Widget::RecvServerMSG() { while(UdpSocket->hasPendingDatagrams()) //is read data { QByteArray data; data.resize(UdpSocket->pendingDatagramSize()); UdpSocket->readDatagram(data.data(), data.size()); //read data QString strText = QString(data); ui->textEdit->insertPlainText(strText + "\r\n"); } } void Widget::SendToClient() { QMessageBox box; QString strSendText = ui->textEdit->toPlainText(); QByteArray datagram = strSendText.toUtf8(); if(strSendText.length() == 0) { box.setText("Please Input text to send!"); } int retLen = UdpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::LocalHost, 7755); if(retLen) { box.setText("send success!"); } box.exec(); }
结果: