Qtcpsocket continuous sending data and continuous receiving data _QT

Source: Internet
Author: User
Tags emit

About this problem for a long time, before doing small exercises, with Qtcpsocket write () a data, and then the receiver as long as emit a readyread () signal and then use Qtcpsocket Read (). The receiver responds to the readyread signal as soon as the sender is write once. In fact, this is not the case, Readyread will not know that the sender called several write, it is only responsible for the arrival of data to trigger, and when you really receive, you may have been able to receive all the data. This depends on the amount of information you send, if very few, such as sending a few bytes of data of course no problem, once Readyread can read all the data but remember, this is not inevitable. In addition Readread can read how much data is uncertain, it depends on your speed, and your computer's performance.

For example, I am programming with the sender while () continuously to the network data transmission, the file data volume is very large, it is necessary to use Waitforbyteswritten to do a wait, This is when the last write () is finished before continuing with the next write () so that the data is not leaking or confusing. There is also a problem is to transfer data must first tell the receiver how much data is sent, that is, when the real data in the head with 2 bytes (quint16) or four bytes (quint32) to store the size of the transmitted data. The receiver is based on the size of the transmission data and the size of each bytesavailable () In contrast if the small on the constant trigger emit signal, in to receive, know the final received total data and the sender's data size is consistent with the instructions received completed. This solves the problem of sending and receiving large amount of data continuously.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.