Qt UDP传送图片,server发送,client接收。
server
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
#define TRAN_SIZE 1024;
struct ImageFrameHead
{
int funCode; //!功能码
unsigned int uTransFrameHdrSize; //!sizeof(WIFI_FRAME_HEADER)
unsigned int uTransFrameSize; //!sizeof(WIFI_FRAME_HEADER) + Data Size
//数据帧变量
unsigned int uDataFrameSize; //数据帧的总大小
unsigned int uDataFrameTotal; //一帧数据被分成传输帧的个数
unsigned int uDataFrameCurr; //数据帧当前的帧号
unsigned int uDataInFrameOffset; //数据帧在整帧的偏移
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setPixelSize(16);
setFont(font);
setWindowTitle(QStringLiteral("UDP Server"));
ui->label->setMaximumHeight(ui->pushButton->sizeHint().height());
mUdpSocket = new QUdpSocket(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
mFlag = true;
mUdpSocket->close();
}
void MainWindow::on_pushButton_clicked()
{
QString name;
int sendCount = 1;
int size = 0;
int num = 0;
int count = 0;
int endSize = 0;
ImageFrameHead mes;
while(1)
{
name = QStringLiteral("./data/%1.jpg").arg(sendCount, 3, 10, QLatin1Char('0'));
mFile.setFileName(name);
mFile.open(QIODevice::ReadOnly);
char *m_sendBuf = new char[1024];
size = mFile.size();
num = 0;
count = 0;
endSize = size%996;
if (endSize == 0)
{
num = size/996;
}
else
{
num = size/996+1;
}
while (count < num)
{
memset(m_sendBuf, 0, 1024);
mes.funCode = 24;
mes.uTransFrameHdrSize = sizeof(ImageFrameHead);
if ((count+1) != num)
{
mes.uTransFrameSize = 996;
}
else
{
mes.uTransFrameSize = endSize;
}
mes.uDataFrameSize = size;
mes.uDataFrameTotal = num;
mes.uDataFrameCurr = count+1;
mes.uDataInFrameOffset = count*(1024 - sizeof(ImageFrameHead));
mFile.read(m_sendBuf+sizeof(ImageFrameHead), 1024-sizeof(ImageFrameHead));
memcpy(m_sendBuf, (char *)&mes, sizeof(ImageFrameHead));
mUdpSocket->writeDatagram(m_sendBuf, mes.uTransFrameSize+mes.uTransFrameHdrSize, QHostAddress("127.0.0.1"), 9930);
QTime dieTime = QTime::currentTime().addMSecs(1);
while( QTime::currentTime() < dieTime )
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
qDebug() << "";
count++;
}
delete []m_sendBuf;
QTime dieTime = QTime::currentTime().addMSecs(10);
while( QTime::currentTime() < dieTime )
{
QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
}
ui->label->setText(QStringLiteral("%1").arg(sendCount++));
if (sendCount == mMaxFileNum + 1)
{
sendCount = 1;
}
mFile.close();
if (mFlag)
{
break;
}
}
}
void MainWindow::on_pushButton_2_clicked()
{
QImage image("wintersun.jpg");
QImage image2(image.size(), QImage::Format_ARGB32);
for (int i = 1; i <= mMaxFileNum; ++i)
{
QString name = QStringLiteral("%1").arg(i, 3, 10, QLatin1Char('0'));
QPainter painter(&image2);
painter.drawImage(0, 0, image);
QPen pen(Qt::red);
QFont font("微软雅黑", 30, QFont::Bold);
painter.setPen(pen);
painter.setFont(font);
painter.drawText(30, 50, name);
image2.save(QStringLiteral("./data/%1.jpg").arg(name), "jpg", 100);
ui->label_image->setPixmap(QPixmap::fromImage(image2));
QCoreApplication::processEvents(QEventLoop::AllEvents);
}
}
client
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
#include
struct ImageFrameHead
{
int funCode; //!功能码
unsigned int uTransFrameHdrSize; //!sizeof(WIFI_FRAME_HEADER)
unsigned int uTransFrameSize; //!sizeof(WIFI_FRAME_HEADER) + Data Size
//数据帧变量
unsigned int uDataFrameSize; //数据帧的总大小
unsigned int uDataFrameTotal; //一帧数据被分成传输帧的个数
unsigned int uDataFrameCurr; //数据帧当前的帧号
unsigned int uDataInFrameOffset; //数据帧在整帧的偏移
};
class UdpThread : public QThread
{
Q_OBJECT
public:
explicit UdpThread(QObject *parent = 0);
~UdpThread();
protected:
virtual void run();
signals:
void sigRecvOk(char *buf, int len);
public slots:
void slotRecv();
private:
QUdpSocket *m_udpSocket;
char *m_buf;
};
UdpThread::UdpThread(QObject *parent) :
QThread(parent)
{
m_buf = new char[1024*1024];
memset(m_buf, 0, 1024*1024);
}
UdpThread::~UdpThread()
{
m_udpSocket->close();
delete []m_buf;
}
void UdpThread::run()
{
m_udpSocket = new QUdpSocket;
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(slotRecv()), Qt::DirectConnection);
m_udpSocket->bind(QHostAddress("127.0.0.1"), 9930);
exec();
}
void UdpThread::slotRecv()
{
char *recvBuf = new char[1052];
memset(recvBuf, 0, 1052);
while(m_udpSocket->hasPendingDatagrams())
{
memset(recvBuf, 0, 1052);
int size = m_udpSocket->pendingDatagramSize();
m_udpSocket->readDatagram(recvBuf, size);
ImageFrameHead *mes = (ImageFrameHead *)recvBuf;
if (mes->funCode == 24) {
memcpy(m_buf+mes->uDataInFrameOffset, (recvBuf+ sizeof(ImageFrameHead)), mes->uTransFrameSize);
if (mes->uDataFrameCurr == mes->uDataFrameTotal)
{
emit sigRecvOk(m_buf, mes->uDataFrameSize);
}
}
}
delete []recvBuf;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFont font;
font.setPixelSize(16);
setFont(font);
setWindowTitle(QStringLiteral("UDP client"));
m_udpThread = new UdpThread;
connect(m_udpThread, SIGNAL(sigRecvOk(char*,int)), this, SLOT(slotRecv(char*,int)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
m_udpThread->start();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
Q_UNUSED(event);
if (m_udpThread->isRunning())
{
m_udpThread->quit();
}
}
void MainWindow::slotRecv(char * buf, int len)
{
QPixmap pixmap;
pixmap.loadFromData((uchar*)buf, len, "JPG");
ui->label->setPixmap(pixmap);
}