gpt4 book ai didi

c++ - QT中低TCP连接性能问题

转载 作者:可可西里 更新时间:2023-11-01 02:50:07 28 4
gpt4 key购买 nike

我正在从事一个图像处理项目,我从相机获取帧并通过使用 Qt 库构建的 TCP 连接发送帧。 我正在尝试发送 24 帧,但当我运行该程序时,只有 13 帧(大约)到达。

我使用信号和槽来启动与按钮的连接,当连接建立时,计时器启动,我每秒发送 24 帧。

编辑:我切换到单连接服务器,但它对性能影响不大,有时帧也会损坏(?)(我只看到空白帧)

tcpsender.cpp

#include "tcpsender.h"
#include "ui_tcpsender.h"

#include <QtWidgets>
#include <QtNetwork>
#include <QtCore>
#include <QDebug>
#include <QBuffer>
#include <QDataStream>

#define XRES 640
#define YRES 480


TCPSender::TCPSender(QWidget *parent) :
QWidget(parent),
ui(new Ui::TCPSender)
{
ui->setupUi(this);
statusLabel = new QLabel(tr("Ready to send frames on port 6667"));
statusLabel->setWordWrap(true);
startButton = new QPushButton(tr("&Start"));
auto quitButton = new QPushButton(tr("&Quit"));
auto buttonBox = new QDialogButtonBox;
buttonBox->addButton(startButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

socket = new QTcpSocket(this);
connect(startButton, &QPushButton::clicked, this, &TCPSender::startConnection);
connect(quitButton, &QPushButton::clicked, this, &TCPSender::close);
connect(socket, SIGNAL(connected()), SLOT(startSending()));
connect(&timer, &QTimer::timeout, this, &TCPSender::sendFrame);

auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);

setWindowTitle(tr("Broadcast Sender"));
camera = new Camera("/dev/video0", XRES, YRES);

time = QTime::currentTime();

}

TCPSender::~TCPSender()
{
delete ui;
}

bool TCPSender::startConnection()
{
if (socket->state() == QAbstractSocket::UnconnectedState)
{

socket->connectToHost(ui->lineEdit->text(), 6667, QIODevice::WriteOnly);
return socket->waitForConnected();

}
}

void TCPSender::startSending()
{
startButton->setEnabled(false);
timer.start(1000/24);
qDebug()<<"Timer start";
}

bool TCPSender::sendFrame()
{
if(socket->state()==QAbstractSocket::ConnectedState){
auto frame = camera->frame();

image = new QImage(frame.data,XRES,YRES,QImage::Format_RGB888);
QImage im = image->convertToFormat(QImage::Format_Grayscale8);
QByteArray ba;
QBuffer buffer(&ba);
im.save(&buffer,"BMP");

qDebug()<<"writing socket";
socket->write(ba);

int speed = time.msecsTo(QTime::currentTime());
time = QTime::currentTime();
speed = 1000*300/speed;
ui->label->setText(QString("%1 kb/s").arg(speed));
delete image;

}

}

receiver.cpp

#include "reciever.h"    
#include <QBuffer>
#include <QTcpSocket>
#include <QImage>
#include <QDebug>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <unistd.h>

Reciever::Reciever(QObject* parent): QTcpServer(parent)
{
connect(this, SIGNAL(newConnection()), this, SLOT(addConnection()));
}

void Reciever::addConnection()
{
qDebug()<<"Adding Connection";
QTcpSocket* connection = nextPendingConnection();
connect(connection, SIGNAL(readyRead()),
this, SLOT(receiveImage()));

connections.append(connection);
QBuffer* buffer = new QBuffer(this);
buffer->open(QIODevice::ReadWrite);
buffers.insert(connection, buffer);

}

void Reciever::receiveImage()
{
qDebug()<<"RECIEVE";
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
QBuffer* buffer = buffers.value(socket);
qint64 bytes = buffer->write(socket->readAll());
emit sendBuffer(buffer,bytes);
}

最佳答案

您在每次计时器滴答时调用 startConnection();。这具有在关闭和重新打开连接之间交替的效果。

  • 您可以在 sendFrame 中删除对 startConnection 的调用,同时保持 TCP 连接打开。

  • 或者将计时器的滴答速率加倍到 timer.start(1000/48);

关于c++ - QT中低TCP连接性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54220526/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com