I am new in QT and SocketProgramming. I want to send char* data to my client. My server sends data with a char* then my client will read that char* and get data from memory (char*'s pointers). I have to read byte by byte and then take the related parts. I mean take message ID from the first 4 bytes, and take the danger priority from the second 4 bytes.
Please help me.
This is my QTcpServer code;
#include "widget.h"
#include "ui_widget.h"
#include "QTcpSocket"
#include "QTcpServer"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
myServer = new QTcpServer(this);
mySocket = nullptr;
connect(myServer, &QTcpServer::newConnection, [&](){
mySocket = myServer->nextPendingConnection();
});
myServer->listen(QHostAddress::Any, 1064);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_sendmessage_clicked()
{
HMI_Msg_EEBL EEBLMsg = HMI_Msg_EEBL();
EEBLMsg.MsgHdr.StartCode = 0xAA55;
EEBLMsg.MsgHdr.Source = 1;
EEBLMsg.MsgHdr.Target = 5;
EEBLMsg.MsgHdr.MsgBdyLength = 12;
EEBLMsg.EEBLBody.MsgID = 102;
if(ui->emergencystatu->currentText()== "1: a")
EEBLMsg.EEBLBody.DangerPriority = 1;
else if(ui->emergencystatu->currentText()== "2: a")
EEBLMsg.EEBLBody.DangerPriority = 2;
else if(ui->emergencystatu->currentText()== "3: a")
EEBLMsg.EEBLBody.DangerPriority = 3;
if(ui->eeblsource->currentText()== "1: b")
EEBLMsg.EEBLBody.EEBLSource = 1;
else if(ui->eeblsource->currentText()== "2: b")
EEBLMsg.EEBLBody.EEBLSource = 2;
char msg2[24];
memcpy(msg2,&EEBLMsg,sizeof(EEBLMsg));
mySocket->write(msg2);
}
This is my QTcpClient code;
#include "widget.h"
#include "ui_widget.h"
#include "QTcpSocket"
#include "QString"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
mySocket = new QTcpSocket(this);
connect(mySocket, &QTcpSocket::readyRead, [&](){
char msg2[24];
mySocket->read(msg2, 2);
});
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_connector_clicked()
{
mySocket->connectToHost("10.10.220.170" , 1064);
}
void Widget::writeEEBL()
{
if(MsgID == 102){
if(DangerPriority == 1){
if(EEBLSource == 1){
mySocket->write("1");
}
if(EEBLSource == 2){
mySocket->write("2");
}
}
}
}