Results 1 to 4 of 4

Thread: Estimate file transfer code via QTcpSocket

  1. #1
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Estimate file transfer code via QTcpSocket

    Hello.
    It was required to do file transfer through QTcpSocket. Googled many different options that confused me.
    The first method was done through the transmission of the length of the block and then the block itself. But then I found a more advanced way through transactions, which appeared in Qt 5.7. I did it on it, asynchronously.
    Please rate whether it is correctly and how much correctly done? If there are any comments, I will be glad to know them. Thank.

    I pass in several variables:
    - type of network packet (file, message or other)
    If the file is:
    - file name
    - file size in bytes
    - data block
    - and at the end a text message

    File Transfer Code:

    Qt Code:
    1. enum PacketType
    2. {
    3. TYPE_NONE = 0,
    4. TYPE_MSG = 1,
    5. TYPE_FILE = 2,
    6. };
    7.  
    8. void TcpClient::socketSendMessage()
    9. {
    10. QDataStream stream(m_pTcpSocket);
    11. stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    12.  
    13. stream << PacketType::TYPE_FILE;
    14.  
    15. QString fileName("/mnt/d/1.png");
    16. QFile file(fileName);
    17. QFileInfo fileInfo(file);
    18. qint64 fileSize = fileInfo.size();
    19.  
    20. stream << fileName;
    21. stream << fileSize;
    22.  
    23. int countSend = 0;
    24.  
    25. if (file.open(QFile::ReadOnly))
    26. {
    27. while(!file.atEnd())
    28. {
    29. QByteArray data = file.read(32768*8);
    30. stream << data;
    31. countSend++;
    32. }
    33. qDebug() << Tools::getTime() << "_CLIENT: ------------------------ countSend FINAL: " << countSend;
    34. }
    35.  
    36. file.close();
    37.  
    38. qDebug() << Tools::getTime() << "_CLIENT: send file ok";
    39.  
    40. QString testStr("TEST_MESSAGE");
    41. stream << testStr;
    42. }
    To copy to clipboard, switch view to plain text mode 

    File retrieval code:
    Header file server:
    Qt Code:
    1. #ifndef MYTCPSERVER_H
    2. #define MYTCPSERVER_H
    3.  
    4. #include <QObject>
    5. #include <QTcpServer>
    6. #include <QTcpSocket>
    7. #include "global.h"
    8. #include <QFile>
    9.  
    10.  
    11. class MyTcpServer : public QObject
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MyTcpServer(QObject *parent = nullptr);
    17. ~MyTcpServer();
    18.  
    19. int number;
    20. QString str;
    21.  
    22. public slots:
    23. void slotNewConnection();
    24. void slotServerRead();
    25. void slotClientDisconnected();
    26. void onSocketReceiveMessage();
    27. void startServer();
    28.  
    29. private:
    30. QTcpServer * mTcpServer;
    31. QTcpSocket * mTcpSocket;
    32. qint64 sizeReceivedData;
    33. QString fileCopy;
    34. PacketType packetType;
    35.  
    36. QString filePath;
    37. qint64 fileSize;
    38. QString testStr;
    39. QByteArray tmpBlock;
    40. int countSend;
    41.  
    42. bool receiveFile(QDataStream &stream);
    43. };
    44.  
    45. #endif // MYTCPSERVER_H
    To copy to clipboard, switch view to plain text mode 

    In the server constructor:
    Qt Code:
    1. packetType = PacketType::TYPE_NONE;
    2. filePath.clear();
    3. fileSize = 0;
    4. testStr.clear();
    5. sizeReceivedData = 0;
    6. tmpBlock.clear();
    7. countSend = 0;
    To copy to clipboard, switch view to plain text mode 

    Message receiving slot:
    Qt Code:
    1. void MyTcpServer::onSocketReceiveMessage()
    2. {
    3. if (!mTcpSocket || !mTcpSocket->bytesAvailable())
    4. return;
    5.  
    6. qDebug() << Tools::getTime() << "SERVER: --------------------new-----------------------";
    7. qDebug() << Tools::getTime() << "SERVER: onSocketReceiveMessage: bytesAvailable" << mTcpSocket->bytesAvailable();
    8.  
    9. QDataStream stream(mTcpSocket);
    10. stream.setVersion(QDataStream::Qt_DefaultCompiledVersion);
    11.  
    12. // Getting PacketType
    13. if (packetType == PacketType::TYPE_NONE) {
    14. stream.startTransaction();
    15. stream >> packetType;
    16. if (!stream.commitTransaction()) {
    17. qDebug() << Tools::getTime() << "SERVER: packetType - FAIL commitTransaction";
    18. return;
    19. }
    20. qDebug() << Tools::getTime() << "SERVER: type:" << packetType;
    21. }
    22.  
    23. if (packetType == PacketType::TYPE_MSG)
    24. {
    25. //
    26. }
    27. else if (packetType == PacketType::TYPE_FILE)
    28. {
    29. //====================================================
    30. // Getting filePath
    31.  
    32. if (filePath.isEmpty()) {
    33. stream.startTransaction();
    34. stream >> filePath;
    35. if (!stream.commitTransaction()) {
    36. qDebug() << Tools::getTime() << "SERVER: filePath - FAIL commitTransaction";
    37. return;
    38. }
    39. qDebug() << Tools::getTime() << "SERVER filePath:" << filePath;
    40. }
    41.  
    42. //====================================================
    43. // Getting fileSize
    44.  
    45. if (!fileSize) {
    46. stream.startTransaction();
    47. stream >> fileSize;
    48. if (!stream.commitTransaction()) {
    49. qDebug() << Tools::getTime() << "SERVER: fileSize - FAIL commitTransaction";
    50. return;
    51. }
    52. qDebug() << Tools::getTime() << "SERVER: fileSize:" << fileSize;
    53. }
    54.  
    55. //====================================================
    56. // Getting file
    57.  
    58. if (sizeReceivedData != fileSize)
    59. {
    60. filePath = this->fileCopy; // temp replace file name
    61. QFile file(filePath);
    62. file.open(QFile::Append);
    63.  
    64. // Work with the file in the loop "while there is data in the socket"
    65. while (!mTcpSocket->atEnd())
    66. {
    67. //====================================================
    68. // Getting tmpBlock
    69.  
    70. stream.startTransaction();
    71. stream >> tmpBlock;
    72.  
    73. if (!stream.commitTransaction()) {
    74. qDebug() << Tools::getTime() << "SERVER: tmpBlock - FAIL commitTransaction";
    75. break;
    76. }
    77.  
    78. qint64 toFile = file.write(tmpBlock);
    79.  
    80. sizeReceivedData += toFile;
    81. countSend++;
    82.  
    83. tmpBlock.clear();
    84.  
    85. if (sizeReceivedData == fileSize)
    86. break;
    87.  
    88. } // while (!mTcpSocket->atEnd())
    89.  
    90. file.close();
    91.  
    92. } // if (sizeReceivedData != fileSize)
    93.  
    94. if (sizeReceivedData != fileSize)
    95. return;
    96.  
    97. qDebug() << Tools::getTime() << "SERVER: sizeReceivedData END: " << sizeReceivedData;
    98. qDebug() << Tools::getTime() << "SERVER fileSize ORIG:" << fileSize;
    99. qDebug() << "SERVER: countSend FINAL: " << countSend;
    100.  
    101.  
    102. //====================================================
    103. // Getting testStr
    104.  
    105. if (testStr.isEmpty()) {
    106. stream.startTransaction();
    107. stream >> testStr;
    108. if (!stream.commitTransaction()) {
    109. qDebug() << Tools::getTime() << "SERVER: testStr - FAIL commitTransaction";
    110. return;
    111. }
    112. qDebug() << Tools::getTime() << "SERVER: testStr:" << testStr;
    113. }
    114.  
    115. qDebug() << Tools::getTime() << "SERVER: END - bytesAvailable:" << mTcpSocket->bytesAvailable();
    116.  
    117. // Clear vars
    118. filePath.clear();
    119. fileSize = 0;
    120. tmpBlock.clear();
    121. sizeReceivedData = 0;
    122. testStr.clear();
    123. countSend = 0;
    124.  
    125. } // else if (packetType == PacketType::TYPE_FILE)
    126. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,508
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Estimate file transfer code via QTcpSocket

    First of all : how does the recipient know the length of the file name? One write on the sender side can generate many reads on receiver side and vice versa.

  3. #3
    Join Date
    Feb 2012
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Estimate file transfer code via QTcpSocket

    Quote Originally Posted by Lesiok View Post
    First of all : how does the recipient know the length of the file name? One write on the sender side can generate many reads on receiver side and vice versa.
    As I understand it, the length of a QString is not transmitted, because Qt itself does this through startTransaction() and commitTransaction(). - https://doc.qt.io/qt-5/qdatastream.h...d-transactions
    or am I wrong?

    Here is just sending a file with a reading block of 10 MB, for 1 time.
    Reading is already more than 1 time.
    Qt Code:
    1. main() 0x7ffeb3ec0fc0
    2. MyTcpServer::startServer() 0x7ffeae040700
    3. TcpClient::startClient() 0x7ffead830700
    4. server is started
    5. ok
    6. "13:45:04.461" _CLIENT: ------------------------ countSend FINAL: 1
    7. "13:45:04.462" _CLIENT: send file ok
    8. CLIENT: "Hello, World!!! I am echo server!\r\n"
    9. "13:45:04.462" SERVER: --------------------new-----------------------
    10. "13:45:04.462" SERVER: onSocketReceiveMessage: bytesAvailable 44
    11. "13:45:04.463" SERVER: type: 2
    12. "13:45:04.463" SERVER filePath: "/mnt/d/1.png"
    13. "13:45:04.464" SERVER: fileSize: 3958548
    14. "13:45:04.464" SERVER: tmpBlock - FAIL commitTransaction
    15. "13:45:04.466" SERVER: --------------------new-----------------------
    16. "13:45:04.467" SERVER: onSocketReceiveMessage: bytesAvailable 524292
    17. "13:45:04.468" SERVER: tmpBlock - FAIL commitTransaction
    18. "13:45:04.470" SERVER: --------------------new-----------------------
    19. "13:45:04.470" SERVER: onSocketReceiveMessage: bytesAvailable 1048580
    20. "13:45:04.472" SERVER: tmpBlock - FAIL commitTransaction
    21. "13:45:04.473" SERVER: --------------------new-----------------------
    22. "13:45:04.473" SERVER: onSocketReceiveMessage: bytesAvailable 1572868
    23. "13:45:04.475" SERVER: tmpBlock - FAIL commitTransaction
    24. "13:45:04.476" SERVER: --------------------new-----------------------
    25. "13:45:04.476" SERVER: onSocketReceiveMessage: bytesAvailable 2097156
    26. "13:45:04.479" SERVER: tmpBlock - FAIL commitTransaction
    27. "13:45:04.480" SERVER: --------------------new-----------------------
    28. "13:45:04.480" SERVER: onSocketReceiveMessage: bytesAvailable 2621444
    29. "13:45:04.482" SERVER: tmpBlock - FAIL commitTransaction
    30. "13:45:04.483" SERVER: --------------------new-----------------------
    31. "13:45:04.483" SERVER: onSocketReceiveMessage: bytesAvailable 3145732
    32. "13:45:04.486" SERVER: tmpBlock - FAIL commitTransaction
    33. "13:45:04.487" SERVER: --------------------new-----------------------
    34. "13:45:04.488" SERVER: onSocketReceiveMessage: bytesAvailable 3670020
    35. "13:45:04.490" SERVER: tmpBlock - FAIL commitTransaction
    36. "13:45:04.491" SERVER: --------------------new-----------------------
    37. "13:45:04.491" SERVER: onSocketReceiveMessage: bytesAvailable 3958580
    38. "13:45:04.495" SERVER: sizeReceivedData END: 3958548
    39. "13:45:04.496" SERVER fileSize ORIG: 3958548
    40. SERVER: countSend FINAL: 1
    41. "13:45:04.496" SERVER: testStr: "TEST_MESSAGE"
    42. "13:45:04.497" SERVER: END - bytesAvailable: 0
    To copy to clipboard, switch view to plain text mode 
    Last edited by Kempo; 18th May 2020 at 13:23.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,508
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Estimate file transfer code via QTcpSocket

    OK, it is my mistake.

Similar Threads

  1. Datatype transfer over qtcpsocket
    By goranpavles in forum Newbie
    Replies: 2
    Last Post: 23rd June 2016, 20:08
  2. Image transfer using QTcpsocket
    By anmol2701 in forum Qt Programming
    Replies: 4
    Last Post: 18th June 2015, 15:06
  3. QTcpSocket problem in file transfer
    By omprakash in forum Qt Programming
    Replies: 6
    Last Post: 25th January 2010, 08:18
  4. File Transfer with QTcpServer and QTcpSocket
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 17:12
  5. QTcpSocket & transfer errors
    By jkam in forum Qt Programming
    Replies: 1
    Last Post: 20th July 2008, 20:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.