目錄
1、Udp服務器
1.1、頭文件
#ifndef UDPSEVER_H
#define UDPSEVER_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UDPSever : public QDialog
{
Q_OBJECT
public:
UDPSever(QWidget *parent = 0);
~UDPSever();
private:
QLabel *TimerLabel;
QLineEdit *TextLineEdit;
QPushButton *StartBtn;
QVBoxLayout *mainLayout;
private slots:
void StartBtnFunc();
void timeOut();
private:
int port;
bool isStarted;
QUdpSocket *udpSocket;
QTimer *timer;
};
#endif // UDPSEVER_H
1.2、定義
#include "udpsever.h"
#include <QHostAddress>
UDPSever::UDPSever(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("UDP Serser"));
TimerLabel = new QLabel(tr("計時器:"),this);
TextLineEdit = new QLineEdit(this);
StartBtn = new QPushButton(tr("開始"),this);
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(TimerLabel);
mainLayout->addWidget(TextLineEdit);
mainLayout->addWidget(StartBtn);
connect(StartBtn,SIGNAL(clicked()),this,SLOT(StartBtnFunc()));
//設置UDP的端口號參數,服務器定時向此端口發送廣播
port = 5555;
isStarted = false;
udpSocket = new QUdpSocket(this);
timer = new QTimer(this);
//計時器輸出信號
connect(timer,SIGNAL(timeout()),this,SLOT(timeOut()));
}
UDPSever::~UDPSever()
{
}
void UDPSever::StartBtnFunc()
{
if(!isStarted)
{
StartBtn->setText(tr("停止"));
//計時器設置,一秒輸出一次信號
timer->start(1000);
isStarted = true;
}
else
{
StartBtn->setText(tr("開始"));
isStarted = false;
timer->stop();
}
}
void UDPSever::timeOut()
{
//獲取文本框內容
QString msg = TextLineEdit->text();
int ilength = 0;
if("" == msg)
{
return;
}
//數據(Latin-1,8位字符串),長度,主機地址,端口號
if((ilength = udpSocket->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::Broadcast,port))
!= msg.length())
{
return;
}
}
2、Udp客戶端
2.1 頭文件
#ifndef UDPCLIENT_H
#define UDPCLIENT_H
#include <QDialog>
#include <QVBoxLayout>
#include <QTextEdit>
#include <QPushButton>
#include <QUdpSocket>
class UdpClient : public QDialog
{
Q_OBJECT
public:
UdpClient(QWidget *parent = 0);
~UdpClient();
private:
QTextEdit *ReceiveText;
QPushButton *CloseBtn;
QVBoxLayout *mainLayout;
public slots:
void CloseBtnFunc();
void dataRecieved();
private:
int port;
QUdpSocket *UdpSocket;
};
#endif // UDPCLIENT_H
2.2 定義
#include "udpclient.h"
#include <QMessageBox>
#include <QHostAddress>
UdpClient::UdpClient(QWidget *parent)
: QDialog(parent)
{
setWindowTitle(tr("UDP Client"));
ReceiveText = new QTextEdit(this);
CloseBtn = new QPushButton(this);
mainLayout = new QVBoxLayout(this);
mainLayout->addWidget(ReceiveText);
mainLayout->addWidget(CloseBtn);
connect(CloseBtn,SIGNAL(clicked()),this,SLOT(CloseBtnFunc()));
port = 5555;
UdpSocket = new QUdpSocket(this);
//QUdpSocket是I/O設備,當有數據到達時,發出readyRead()信號
connect(UdpSocket,SIGNAL(readyRead()),this,SLOT(dataRecieved()));
//綁定端口
bool bResult = UdpSocket->bind(port);
if(!bResult)
{
QMessageBox::information(this,tr("error"),tr("udp socket create error"));
return;
}
}
UdpClient::~UdpClient()
{
}
void UdpClient::CloseBtnFunc()
{
close();
}
void UdpClient::dataRecieved()
{
//當有數據報是 返回true
while(UdpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(UdpSocket->pendingDatagramSize());
//讀取數據報
UdpSocket->readDatagram(datagram.data(),datagram.size());
QString msg = datagram.data();
//顯示數據報內容(無格式的按順序在文本中顯示內容)
ReceiveText->insertPlainText(msg);
}
}