Results 1 to 3 of 3

Thread: The QTcpSocket has been terminated when writing large data

  1. #1
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Question The QTcpSocket has been terminated when writing large data

    When i writing large data,for example 18MB data over QTcpSocket,the QTcpSocket terminated in Writing process...
    first i used this code for writing:
    Qt Code:
    1. client_socket->write(packet,packet.size());
    2. client_socket->waitForBytesWritten();
    To copy to clipboard, switch view to plain text mode 
    above code is not work correctly,and then i use this code for sending:
    Qt Code:
    1. if (packet.size()>25344)
    2. {
    3. QDataStream data_stream(packet);
    4. data_stream.setVersion(QDataStream::Qt_5_3);
    5. while (!data_stream.atEnd())
    6. {
    7. int len=1024;
    8. char buffer[len];
    9. int size_read=data_stream.readRawData(buffer,len);
    10. if (size_read==-1)
    11. cout<<"ERROR!";
    12. client_socket->write(buffer,size_read);
    13. client_socket->waitForBytesWritten();
    14. }
    15. }
    16. else
    17. {
    18. client_socket->write(packet,packet.size());
    19. client_socket->waitForBytesWritten();
    20. }
    To copy to clipboard, switch view to plain text mode 

    and in another hand i using this code for reading data :
    Qt Code:
    1. void server::readyRead()
    2. {
    3.  
    4. QTcpSocket *i_client=(QTcpSocket*) sender();
    5. quint16 packet_size=i_client->bytesAvailable();//Read Count Bytes waiting on line
    6. qDebug()<<"Packet Recived,Packet len: "<<QString::number(packet_size)<<"\n";
    7.  
    8. if (packet_size>0)
    9. {
    10.  
    11. QByteArray recive_bytes=i_client->read(packet_size);//Read Packet
    12. //...
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    second way is better,but in both of tow methods ,QTcpSocket terminated after sending some bytes!
    what's the problem!?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,626
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: The QTcpSocket has been terminated when writing large data

    What is terminated? The sender or receiver? Terminated how? Crash or simply stop? Have you single-stepped through the code in your debugger or inspected the stack backtrace?

    In your second sender what occurs after the byte array is exhausted and your read fails?

    What happens at the server when there are more than 65536 bytes available (line 5)? What is the return type of QTcpSocket::bytesAvailable()?


    Do you really want to block your entire program while 18MB is sent?

  3. The following user says thank you to ChrisW67 for this useful post:

    ms2222 (6th December 2014)

  4. #3
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: The QTcpSocket has been terminated when writing large data

    oh,thank's,it's my fault,QTcpSocket::bytesAvailable() return qint64 but i used quint16 !
    Do you really want to block your entire program while 18MB is sent?
    yes,in this project is not care program is block...

Similar Threads

  1. QTcpSocket Writing Data Progress
    By lwinhtooko in forum Qt Programming
    Replies: 2
    Last Post: 1st November 2010, 20:10
  2. QTcpSocket writing reading problem
    By Misenko in forum Qt Programming
    Replies: 4
    Last Post: 15th October 2008, 07:27
  3. Compress and Uncompress large files in QTcpSocket
    By vishesh in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2007, 22:21
  4. fast writing of large amounts of data to files
    By TheKedge in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2007, 16:33
  5. Replies: 6
    Last Post: 8th January 2007, 10:24

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.