gpt4 book ai didi

c++ - 通过 Qt NamedPipe 发送数据时追加数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:00 25 4
gpt4 key购买 nike

使用QT通过Namedpipe发送数据时出现数据追加问题

本地服务器.h

#ifndef LOCALSERVER_H
#define LOCALSERVER_H

#include "QLocalSocket"
#include "QLocalServer"

class QLocalSocket;

class LocalServer : public QLocalServer
{
Q_OBJECT
public:
explicit LocalServer(QObject *parent = 0);
void send(QString &msj);
QLocalSocket *mSocket;


private slots:
void receive();

private:

};

#endif // LOCALSERVER_H

本地服务器.cpp

#include "localserver.h"
#include "QLocalSocket"
#include "QTextStream"
#include "QMessageBox"
LocalServer::LocalServer(QObject *parent) : QLocalServer(parent)
{
mSocket = nullptr;
connect(this,&LocalServer::newConnection,[&](){
mSocket = nextPendingConnection();
});
qDebug()<<"Hello World";
}

void LocalServer::send(QString &msj)
{
qDebug()<<"Sending Data";
if(mSocket)
{
QTextStream T(mSocket);
T<<msj.toStdString().c_str();
mSocket->flush();
}

}

void LocalServer::receive()
{
qDebug()<<"Im here";
QDataStream in(mSocket);
in.setVersion(QDataStream::Qt_5_2);
mSocket->readAll();
qDebug()<<"Data Received";
}

小部件.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "QProcess"
#include "QTimer"
#include "QLocalSocket"
#include "localserver.h"
namespace Ui {
class Widget;
}



class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

void Connection();
private slots:
void on_start_clicked();

void on_send_clicked();

void on_quit_clicked();


void detect_connection();

void connect_terminate();

void connected();
void send_data();
void receive_data();


QString RandomData();
void on_pbtn_Stop_clicked();

private:
Ui::Widget *ui;
LocalServer *mLocalServer;
QTimer *timer;
QLocalSocket *mSocket;
bool first;
QString data_received;

};

#endif // WIDGET_H

小部件.cpp

#include "widget.h"
#include "ui_widget.h"
#include "QProcess"
#include "QDebug"
#include "localserver.h"
#include "QMessageBox"

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
Connection();
first = true;
}
void Widget::Connection()
{
mLocalServer = new LocalServer(this);
mLocalServer->listen("Server1");
connect(mLocalServer,SIGNAL(newConnection()),this,SLOT(detect_connection()));

first = true;

timer = new QTimer(this);
timer->setInterval(8);
connect(timer,SIGNAL(timeout()),this,SLOT(send_data()));

}

Widget::~Widget()
{

delete ui;

}

void Widget::on_start_clicked()
{
timer->start();

}

void Widget::on_send_clicked()
{
timer->stop();
}

void Widget::on_quit_clicked()
{
timer->stop();
this->close();
}



void Widget::detect_connection()
{
qDebug()<<"Write Pipe Established";
mSocket = new QLocalSocket(this);
mSocket->connectToServer("Server2");
connect(mSocket,SIGNAL(disconnected()),this,SLOT(connect_terminate()));
connect(mSocket,SIGNAL(connected()),this,SLOT(connected()));

qDebug()<<"Connection Established, Ready to Read";
connect(mSocket,&QLocalSocket::readyRead, [&]() {
QTextStream T(mSocket);
ui->receivedText->addItem(T.readAll());
});
}

void Widget::connect_terminate()
{
qDebug()<<"Read Server Connection Terminated";
QString string = "Stop_Session";
mLocalServer->send(string);
ui->sentText->addItem(string);
}




void Widget::connected()
{
qDebug()<<"Socket Connected to Server";
QString s=" Connection Done ";
mLocalServer->send(s);
}

void Widget::send_data()
{
QString string = RandomData();
mLocalServer->send(string);
ui->sentText->addItem(string);
}
QString Widget::RandomData()
{
srand(time(NULL));
int random_number = rand()%(1920-0 + 1) + 0; // rand() return a number between ​0​ and RAND_MAX
int random_number1 = rand()%(1080-0 + 1) + 0;
int random_number2 = rand()%(10-0 + 1) + 0;
QString s="Contour,"+QString::number(random_number)+","+QString::number(random_number1)+","+QString::number(random_number2);
// Basically this data in "s" gives the actual contours detected data but am using a random generator to test the Local Server and Socket .


return s;
}

void Widget::receive_data()
{
QString string;
QTextStream T(mSocket);
T.readLine();
if(T.readAll()=="Stop")
{
qDebug()<<"Socket About to Close";
QString string = "Stop";
mLocalServer->send(string);
}
else
{
qDebug()<<"Program can continue"<<T.readAll();
}
}

void Widget::on_pbtn_Stop_clicked()
{
timer->stop();
}

在接收端程序

接收器.cpp

void Receiver ::Declaration_Of_Server2()
{
// Declaration Of LocalServer i.e Server2
ServerIS = new LocalServer(this);
ServerIS->listen("Server2");
// Connect To a LocalSocket TO The Above LocalServer i.e Server_IS with Server_VS
connect(ServerIS,SIGNAL(newConnection()),this,SLOT(DetectConnection_WithServer1()));
}

void CoFire_MainPage::DetectConnection_WithServer1()
{
// Declaration of LocalSocket i.e Server_VS
SocketIS = new QLocalSocket(this);
SocketIS->connectToServer("Server1");
connect(SocketIS,SIGNAL(readyRead()),this,SLOT(Receiving_VS_Data()));
}

void CoFire_MainPage::Receiving_VS_Data()
{
// Receiving Vs Data From Socket "Socket_IS"
QTextStream msg(SocketIS);
QString str = msg.readLine();
qDebug()<<" VS DATA : "<<str;
}

通过这个我试图将数据(例如:Contour,1000,800,1)从一个程序发送到另一个程序速度为 1 个数据/8 毫秒,但在实际情况下,它甚至可能达到 1 毫秒的数据传输。但是通过这个在接收端时不时地附加输入数据 (例如:Contour,1000,800,1Contour,200,400,1Contour,500,650,1 等...)。但实际上预期的数据是第一轮廓,1000,800,1下一个轮廓,200,400,1下一个轮廓,500,650,1等等……

这样,在我在接收方捕获和处理数据之前,数据就会被附加。就这样失去了宝贵的情报。我该如何解决这个问题。

最佳答案

readLine()等待行尾("\n""\r\n"),但在您的情况下您不发送它,您必须更改为以下内容:

QString Widget::RandomData(){
srand(time(NULL));
int random_number = rand()%(1920-0 + 1) + 0; // rand() return a number between ​0​ and RAND_MAX
int random_number1 = rand()%(1080-0 + 1) + 0;
int random_number2 = rand()%(10-0 + 1) + 0;
return QString("Contour,%1,%2,%3\n")
.arg(random_number)
.arg(random_number1)
.arg(random_number2);
}

另一方面,没有必要使用QString的引用,也没有必要使用toStdString().c_str()

void LocalServer::send(const QString &msj)
{
qDebug()<<"Sending Data";
if(mSocket)
{
QTextStream T(mSocket);
T << msj;
mSocket->flush();
}

}

关于c++ - 通过 Qt NamedPipe 发送数据时追加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51581562/

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