//.hTripServer *server;//.cpp server_in=new TripServer(this,&gbo_data) ; if (!server_in->listen(QHostAddress::Any, 257)) { qDebug()<< "Failed to bind to port"; }
#include <QtCore>#include "clientsocket.h"#include "tripserver.h"TripServer::TripServer(QObject *parent,_DATA_15GBO *dataGbo) : QTcpServer(parent){ data=dataGbo;}void TripServer::incomingConnection(int socketId){ ClientSocket *socket = new ClientSocket(this, this->serverPort(),data); socket->setSocketDescriptor(socketId);}
#include <QtNetwork>#include "clientsocket.h"unsigned char numb_packet=0;ClientSocket::ClientSocket(QObject *parent, int port,_DATA_15GBO *GboStr) : QTcpSocket(parent){ connect(this, SIGNAL(readyRead()), this, SLOT(readClient())); connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater())); nextBlockSize = 0; dataGbo=GboStr; if(port==259) { tim=new QTimer(this); connect(tim,SIGNAL(timeout()),SLOT(slotSendDataGBO())); tim->start(500); }}void ClientSocket::readClient(){ QByteArray arr; QDataStream in(this); in.setVersion(QDataStream::Qt_4_2); quint64 tt=0; if (nextBlockSize == 0) { if (bytesAvailable() < sizeof(quint16)) return; tt=bytesAvailable(); arr=read(tt); tt=arr.size(); command_arr=arr.mid(2,arr.size()-2); qDebug()<<"in data="<< command_arr; //ВОТ ЭТИ ДАННЫЕ emit sendDataGbo(); } close(); processPaketFromGBO(command_arr);}
typedef struct{ unsigned char head[2]; unsigned char bort; unsigned char number_packet; unsigned char fatal; unsigned char param_byte1; unsigned char param_byte2; unsigned char param_byte3; unsigned short press; unsigned short count_body; unsigned char summa; } _HEAD_15GBO;
C++ (Qt)struct CHeader { int command; int dataSize;};... // ждем пока придут данные заголовка if (bytesAvailable() < sizeof(CHeader)) msleep(100); // читаем заголовокCHeader head;QByteArray arr = read(sizeof(CHeader)); // десериализуем заголовокif (1) { QDataStream strm(arr); strm >> head.command; strm >> head.dataSize;} // проверяем что за командаswitch (head.command) {...} // тупо ждем данных (в заголовке есть их длина)if (bytesAvailable() < head.dataSize) msleep(100); // все данные пришли, читаемarr = read(head.dataSize); // десериализуем данные if (1) { QDataStream strm(arr); strm >> ...}