Results 1 to 12 of 12

Thread: Copy with QDataStream

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #11
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Copy with QDataStream

    Quote Originally Posted by wirasto View Post
    Not work. But thank's

    Qt Code:
    1. QTcpSocket socket;
    2. if( !socket.setSocketDescriptor( m_descriptor ) )
    3. {
    4. qDebug( "Socket error!" );
    5. return;
    6. }
    7.  
    8.  
    9. QFile file("images/linux.png");
    10. if (!file.open(QIODevice::ReadOnly))
    11. {
    12. qDebug("Cannot open file!");
    13. return;
    14. }
    15.  
    16. QByteArray data=file.readAll();
    17. int size=data.size();
    18.  
    19. for(int i=0; i< 4; ++i)
    20. {
    21. quint8 b=(size >> (i*8)) & 0x000000ff;
    22. data.append(b);
    23. }
    24.  
    25. socket.write(data);
    26. socket.disconnectFromHost();
    To copy to clipboard, switch view to plain text mode 
    Men you have appended your size to the end of the data! What is the sense of doing that if you have to know the size at the very beginning of receiving data??? You have to send the size, and then the data:
    Qt Code:
    1. QTcpSocket socket;
    2. if( !socket.setSocketDescriptor( m_descriptor ) )
    3. {
    4. qDebug( "Socket error!" );
    5. return;
    6. }
    7.  
    8.  
    9. QFile file("images/linux.png");
    10. if (!file.open(QIODevice::ReadOnly))
    11. {
    12. qDebug("Cannot open file!");
    13. return;
    14. }
    15.  
    16. QByteArray data=file.readAll();
    17. int size=data.size();
    18.  
    19. QByteArray sizeBytes;
    20. for(int i=0; i< 4; ++i)
    21. {
    22. quint8 b=(size >> (i*8)) & 0x000000ff;
    23. sizeBytes.append(b);
    24. }
    25.  
    26. sizeBytes.append(data);
    27. socket.write(sizeBytes);
    28. socket.disconnectFromHost();
    To copy to clipboard, switch view to plain text mode 
    it is not that hard to invent that idea.
    And you dont even use that information about size on the client side.
    Tell me what happend if you do it like this:
    Qt Code:
    1. void Dialog::tcpReady()
    2. {
    3. int bytesAvail = socket.bytesAvailable() - 4;
    4. QByteArray ar=socket.read(4);
    5. quint32 size=0;
    6. for(int i=0; i < 4; ++i)
    7. {
    8. quint8 b=ar.at(i);
    9. size |= (((quint32)b) & 0x000000ff) << i*8;
    10. }
    11.  
    12. if (size > bytesAvail)
    13. qDebug("Different sizes!!!");
    14. else
    15. qDebug("All data have been delivered!");
    16.  
    17. ar = socket.readAll();
    18. QFile file("linuxmu.png");
    19. file.open(QIODevice::WriteOnly);
    20. file.write(ar);
    21.  
    22. file.close();
    23. }
    To copy to clipboard, switch view to plain text mode 

    I think not right to me for learn about QtNetwork now.
    As I see you have to read about network at all...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  2. The following user says thank you to faldzip for this useful post:

    wirasto (9th July 2009)

Similar Threads

  1. Replies: 1
    Last Post: 27th November 2014, 09:11
  2. Q_INTERFACES with classes having a copy constructor
    By Mike in forum Qt Programming
    Replies: 0
    Last Post: 31st October 2008, 12:40
  3. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  4. copy and run a .exe file in another system
    By sabeesh in forum Installation and Deployment
    Replies: 3
    Last Post: 22nd August 2007, 10:05
  5. QDataStream >> QString
    By smalls in forum Qt Programming
    Replies: 2
    Last Post: 17th January 2006, 22:14

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
  •  
Qt is a trademark of The Qt Company.