#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QBuffer>
#include <QImage>
#include <QDebug>
#include <QHostInfo>
#include <QNetworkInterface>
#include <QCoreApplication>
#include <QTime>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->lineEditIP->setText("192.168.1.25");
ui->lineEditPort->setText("25000");
ui->lineEditIP_2->setText(getLocalIP());
ui->lineEditPort_2->setText("25000");
udpsocketSend=new QUdpSocket(this);
udpSocketRecv = new QUdpSocket(this);
udpSocketRecv->bind(ui->lineEditPort_2->text().toUInt(), QUdpSocket::ShareAddress);
connect(udpSocketRecv, SIGNAL(readyRead()), this, SLOT(ReadPendingDatagrams()));
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(slotSendImage()));
QString picPath=QCoreApplication::applicationDirPath()+"/test.jpg";
pixmap.load(picPath);
ui->lineEditFramePerSecond->setText("1");
sendImageNum=0;
recvImageNum=0;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
timer->start(1000/ui->lineEditFramePerSecond->text().toUInt());
}
void MainWindow::on_pushButton_2_clicked()
{
timer->stop();
}
void MainWindow::on_pushButton_3_clicked()
{
ui->lineEditSendFrame->clear();
sendImageNum=0;
}
void MainWindow::on_pushButton_4_clicked()
{
ui->lineEditRevFrame->clear();
recvImageNum=0;
}
void MainWindow::slotSendImage()
{
matrix.rotate(90);
QPixmap tempPixmap = pixmap.transformed(matrix);
QBuffer buffer;
tempPixmap.save(&buffer,"jpg");
ui->labelImage->setPixmap(tempPixmap);
//不分包
// udpsocketSend->writeDatagram(buffer.data().data(), buffer.data().size(),
// QHostAddress(ui->lineEditIP->text()), ui->lineEditPort->text().toInt());
//分包开始+++++++++++++++++++++++++++
int dataLength=buffer.data().size();
unsigned char *dataBuffer=(unsigned char *)buffer.data().data();
int packetNum = 0;
int lastPaketSize = 0;
packetNum = dataLength / UDP_MAX_SIZE;
lastPaketSize = dataLength % UDP_MAX_SIZE;
int currentPacketIndex = 0;
if (lastPaketSize != 0)
{
packetNum = packetNum + 1;
}
PackageHeader packageHead;
packageHead.uTransPackageHdrSize=sizeof(packageHead);
packageHead.uDataSize = dataLength;
packageHead.uDataPackageNum = packetNum;
unsigned char frameBuffer[1024*1000];
memset(frameBuffer,0,1024*1000);
while (currentPacketIndex < packetNum)
{
if (currentPacketIndex < (packetNum-1))
{
packageHead.uTransPackageSize = sizeof(PackageHeader)+UDP_MAX_SIZE;
packageHead.uDataPackageCurrIndex = currentPacketIndex+1;
packageHead.uDataPackageOffset = currentPacketIndex*UDP_MAX_SIZE;
memcpy(frameBuffer, &packageHead, sizeof(PackageHeader));
memcpy(frameBuffer+sizeof(PackageHeader), dataBuffer+packageHead.uDataPackageOffset, UDP_MAX_SIZE);
int length=udpsocketSend->writeDatagram(
(const char*)frameBuffer, packageHead.uTransPackageSize,
QHostAddress(ui->lineEditIP->text()), ui->lineEditPort->text().toInt());
if(length!=packageHead.uTransPackageSize)
{
qDebug()<<"Failed to send image";
}
currentPacketIndex++;
}
else
{
packageHead.uTransPackageSize = sizeof(PackageHeader)+(dataLength-currentPacketIndex*UDP_MAX_SIZE);
packageHead.uDataPackageCurrIndex = currentPacketIndex+1;
packageHead.uDataPackageOffset = currentPacketIndex*UDP_MAX_SIZE;
memcpy(frameBuffer, &packageHead, sizeof(PackageHeader));
memcpy(frameBuffer+sizeof(PackageHeader), dataBuffer+packageHead.uDataPackageOffset, dataLength-currentPacketIndex*UDP_MAX_SIZE);
int length=udpsocketSend->writeDatagram(
(const char*)frameBuffer, packageHead.uTransPackageSize,
QHostAddress(ui->lineEditIP->text()), ui->lineEditPort->text().toInt());
if(length!=packageHead.uTransPackageSize)
{
qDebug()<<"Failed to send image";
}
currentPacketIndex++;
}
}
//分包结束+++++++++++++++++++++++++++
sendImageNum++;
ui->lineEditSendFrame->setText(QString::number(sendImageNum));
ui->lineEditSendSize->setText(QString::number(buffer.data().size()));
}
void MainWindow::ReadPendingDatagrams()
{
QByteArray datagram;
while (udpSocketRecv->hasPendingDatagrams())
{
datagram.resize(udpSocketRecv->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpSocketRecv->readDatagram(
datagram.data(),
datagram.size(),
&sender,
&senderPort);
//不组包
// QImage image;
// image.loadFromData((uchar *)datagram.data(), datagram.size(),"JPG");
// QPixmap pixmap=QPixmap::fromImage(image);
// ui->labelImage_2->setPixmap(pixmap);
// recvImageNum++;
// ui->lineEditRevFrame->setText(QString::number(recvImageNum));
// ui->lineEditRevSize->setText(QString::number(datagram.size()));
//组包开始+++++++++++++++++++++++++++
static int num = 1;
static uint size = 0;
PackageHeader *packageHead = (PackageHeader *)datagram.data();
if (packageHead->uDataPackageCurrIndex == num)
{
num++;
size += packageHead->uTransPackageSize-packageHead->uTransPackageHdrSize;
if (size > 1024*1000)
{
qDebug()<<"image too big";
num = 1;
size = 0;
return;
}
if (packageHead->uDataPackageOffset > 1024*1000)
{
qDebug()<<"image too big";
packageHead->uDataPackageOffset = 0;
num = 1;
size = 0;
return;
}
memcpy(imageData.data+packageHead->uDataPackageOffset, datagram.data()+packageHead->uTransPackageHdrSize,
packageHead->uTransPackageSize-packageHead->uTransPackageHdrSize);
if ((packageHead->uDataPackageNum == packageHead->uDataPackageCurrIndex)
&& (size == packageHead->uDataSize))
{
imageData.length = packageHead->uDataSize;
QImage image;
image.loadFromData((uchar *)imageData.data,imageData.length,"JPG");
QPixmap pixmap=QPixmap::fromImage(image);
ui->labelImage_2->setPixmap(pixmap);
recvImageNum++;
ui->lineEditRevFrame->setText(QString::number(recvImageNum));
ui->lineEditRevSize->setText(QString::number(imageData.length));
memset(&imageData,0,sizeof(UdpUnpackData));
num = 1;
size = 0;
}
}
else
{
num = 1;
size = 0;
memset(&imageData,0,sizeof(UdpUnpackData));
}
//组包结束+++++++++++++++++++++++++++
}
}
QString MainWindow::getLocalIP()
{
QList<QHostAddress> addrs = QNetworkInterface::allAddresses();
for(int i = 0; i < addrs.size(); i++)
{
if (addrs.at(i).protocol() == QAbstractSocket::IPv4Protocol &&
addrs.at(i
- 1
- 2
前往页