Sender
senderdialog.h
#ifndef SENDERDIALOG_H
#define SENDERDIALOG_H
#include <QDialog>
#include <QUdpSocket>
#include <QTimer>
namespace Ui {
class SenderDialog;
}
class SenderDialog : public QDialog
{
Q_OBJECT
public:
explicit SenderDialog(QWidget *parent = 0);
~SenderDialog();
private slots:
void on_pushButton_clicked();
void sendMessage();
private:
Ui::SenderDialog *ui;
bool isStarted;
QUdpSocket* udpSocket;
QTimer* timer;
};
#endif
senderdialog.cpp
#include "senderdialog.h"
#include "ui_senderdialog.h"
SenderDialog::SenderDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SenderDialog)
{
ui->setupUi(this);
isStarted = false;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(sendMessage()));
}
SenderDialog::~SenderDialog()
{
delete ui;
}
void SenderDialog::on_pushButton_clicked()
{
if(isStarted == false){
isStarted = true;
ui->pushButton->setText("停止广播");
ui->messageEdit->setEnabled(false);
ui->portEdit->setEnabled(false);
timer->start(1000);
}
else{
isStarted = false;
ui->pushButton->setText("开始广播");
ui->messageEdit->setEnabled(true);
ui->portEdit->setEnabled(true);
timer->stop();
}
}
void SenderDialog::sendMessage()
{
QString msg = ui->messageEdit->text();
if(msg == ""){
return;
}
quint16 port = ui->portEdit->text().toShort();
if(port<1024){
return;
}
udpSocket->writeDatagram(msg.toUtf8(),QHostAddress::Broadcast,port);
}
main.cpp
#include "senderdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
SenderDialog w;
w.show();
return a.exec();
}
Receiver
receiver.h
#ifndef RECEIVERDIALOG_H
#define RECEIVERDIALOG_H
#include <QDialog>
#include <QUdpSocket>
#include <QMessageBox>
namespace Ui {
class ReceiverDialog;
}
class ReceiverDialog : public QDialog
{
Q_OBJECT
public:
explicit ReceiverDialog(QWidget *parent = 0);
~ReceiverDialog();
private slots:
void on_pushButton_clicked();
void receiveMessage();
private:
Ui::ReceiverDialog *ui;
bool isStarted;
QUdpSocket* udpSocket;
};
#endif
receiverdialog.cpp
#include "receiverdialog.h"
#include "ui_receiverdialog.h"
ReceiverDialog::ReceiverDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ReceiverDialog)
{
ui->setupUi(this);
isStarted = false;
udpSocket = new QUdpSocket(this);
}
ReceiverDialog::~ReceiverDialog()
{
delete ui;
}
void ReceiverDialog::on_pushButton_clicked()
{
if(isStarted == false){
isStarted = true;
ui->pushButton->setText("停止接收");
quint16 port = ui->lineEdit->text().toShort();
ui->lineEdit->setEnabled(false);
if(udpSocket->bind(QHostAddress::Any,port)==false){
QMessageBox::critical(this,"Error","端口号错误!");
return;
}
else{
qDebug("绑定端口成功!");
connect(udpSocket,SIGNAL(readyRead()),
this,SLOT(receiveMessage()));
}
}
else{
isStarted = false;
ui->pushButton->setText("开始接收");
udpSocket->close();
ui->lineEdit->setEnabled(true);
}
}
void ReceiverDialog::receiveMessage()
{
while(udpSocket->hasPendingDatagrams()){
QByteArray buf;
buf.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(buf.data(),buf.size());
ui->listWidget->addItem(buf);
ui->listWidget->scrollToBottom();
}
}
main.cpp
#include "receiverdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ReceiverDialog w;
w.show();
return a.exec();
}
|