enum PacketType{ TYPE_NONE = 0, TYPE_MSG = 1, TYPE_FILE = 2,};void TcpClient::socketSendMessage(){ QDataStream stream(m_pTcpSocket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); stream << PacketType::TYPE_FILE; QString fileName("/mnt/d/1.png"); QFile file(fileName); QFileInfo fileInfo(file); qint64 fileSize = fileInfo.size(); stream << fileName; stream << fileSize; int countSend = 0; if (file.open(QFile::ReadOnly)) { while(!file.atEnd()) { QByteArray data = file.read(32768*8); stream << data; countSend++; } qDebug() << Tools::getTime() << "_CLIENT: ------------------------ countSend FINAL: " << countSend; } file.close(); qDebug() << Tools::getTime() << "_CLIENT: send file ok"; QString testStr("TEST_MESSAGE"); stream << testStr;}
#ifndef MYTCPSERVER_H#define MYTCPSERVER_H#include <QObject>#include <QTcpServer>#include <QTcpSocket>#include "global.h"#include <QFile>class MyTcpServer : public QObject{ Q_OBJECTpublic: explicit MyTcpServer(QObject *parent = nullptr); ~MyTcpServer(); int number; QString str;public slots: void slotNewConnection(); void slotServerRead(); void slotClientDisconnected(); void onSocketReceiveMessage(); void startServer();private: QTcpServer * mTcpServer; QTcpSocket * mTcpSocket; qint64 sizeReceivedData; QString fileCopy; PacketType packetType; QString filePath; qint64 fileSize; QString testStr; QByteArray tmpBlock; int countSend; bool receiveFile(QDataStream &stream);};#endif // MYTCPSERVER_H
packetType = PacketType::TYPE_NONE; filePath.clear(); fileSize = 0; testStr.clear(); sizeReceivedData = 0; tmpBlock.clear(); countSend = 0;
void MyTcpServer::onSocketReceiveMessage(){ if (!mTcpSocket || !mTcpSocket->bytesAvailable()) return; qDebug() << Tools::getTime() << "SERVER: --------------------new-----------------------"; qDebug() << Tools::getTime() << "SERVER: onSocketReceiveMessage: bytesAvailable" << mTcpSocket->bytesAvailable(); QDataStream stream(mTcpSocket); stream.setVersion(QDataStream::Qt_DefaultCompiledVersion); // Считывание PacketType if (packetType == PacketType::TYPE_NONE) { stream.startTransaction(); stream >> packetType; if (!stream.commitTransaction()) { qDebug() << Tools::getTime() << "SERVER: packetType - FAIL commitTransaction"; return; } qDebug() << Tools::getTime() << "SERVER: type:" << packetType; } if (packetType == PacketType::TYPE_MSG) { // } else if (packetType == PacketType::TYPE_FILE) { //==================================================== // Получение filePath if (filePath.isEmpty()) { stream.startTransaction(); stream >> filePath; if (!stream.commitTransaction()) { qDebug() << Tools::getTime() << "SERVER: filePath - FAIL commitTransaction"; return; } qDebug() << Tools::getTime() << "SERVER filePath:" << filePath; } //==================================================== // Получение fileSize if (!fileSize) { stream.startTransaction(); stream >> fileSize; if (!stream.commitTransaction()) { qDebug() << Tools::getTime() << "SERVER: fileSize - FAIL commitTransaction"; return; } qDebug() << Tools::getTime() << "SERVER: fileSize:" << fileSize; } //==================================================== // Получение файла if (sizeReceivedData != fileSize) { filePath = this->fileCopy; // временная замена имени файла QFile file(filePath); file.open(QFile::Append); // Работа с файлом в цикле "пока в сокете есть данные" while (!mTcpSocket->atEnd()) { //==================================================== // Получение tmpBlock stream.startTransaction(); stream >> tmpBlock; if (!stream.commitTransaction()) { qDebug() << Tools::getTime() << "SERVER: tmpBlock - FAIL commitTransaction"; break; } qint64 toFile = file.write(tmpBlock); sizeReceivedData += toFile; countSend++; tmpBlock.clear(); if (sizeReceivedData == fileSize) break; } // while (!mTcpSocket->atEnd()) file.close(); } // if (sizeReceivedData != fileSize) if (sizeReceivedData != fileSize) return; qDebug() << Tools::getTime() << "SERVER: sizeReceivedData END: " << sizeReceivedData; qDebug() << Tools::getTime() << "SERVER fileSize ORIG:" << fileSize; qDebug() << "SERVER: countSend FINAL: " << countSend; //==================================================== // Получение testStr if (testStr.isEmpty()) { stream.startTransaction(); stream >> testStr; if (!stream.commitTransaction()) { qDebug() << Tools::getTime() << "SERVER: testStr - FAIL commitTransaction"; return; } qDebug() << Tools::getTime() << "SERVER: testStr:" << testStr; } qDebug() << Tools::getTime() << "SERVER: END - bytesAvailable:" << mTcpSocket->bytesAvailable(); // Очистка переменных filePath.clear(); fileSize = 0; tmpBlock.clear(); sizeReceivedData = 0; testStr.clear(); countSend = 0; } // else if (packetType == PacketType::TYPE_FILE)}
int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MyTcpServer *server = new MyTcpServer; QThread *threadServer = new QThread; QObject::connect(threadServer, &QThread::started, server, &MyTcpServer::startServer); QObject::connect(threadServer, &QThread::finished, server, &MyTcpServer::deleteLater); server->moveToThread(threadServer); threadServer->start(); //QThread::msleep(100); //TcpClient *client = new TcpClient("127.0.0.1", 1111); //QThread *threadClient = new QThread; //QObject::connect(threadClient, &QThread::started, client, &TcpClient::startClient); //QObject::connect(threadClient, &QThread::finished, client, &TcpClient::deleteLater); //client->moveToThread(threadClient); //threadClient->start(); return a.exec();}