search

Home  >  Q&A  >  body text

objective-c - GCDAsyncSocket 如何顺序发送???

GCDAsyncSocket 如何顺序发送???比如先发送,文件名,再发长度,再发送文件,还必须要在文件名发送完了之后发送下一个

PHPzPHPz2757 days ago828

reply all(1)I'll reply

  • 巴扎黑

    巴扎黑2017-05-02 09:34:41

    Simply put, TCP connections are out of order during network transmission (mainly to optimize data transmission), but TCP has corresponding "sorting" processing, so for the same connection (such as between client A and server S (this connection), the bottom layer of TCP Socket has implemented the sequence. If you send in the order of a, b, c, the receiver will receive a, b, c in sequence. This is also an advantage of TCP over UDP.

    So your problem should not be a GCDAsyncSocket problem.

    Socket only sends and receives data, without specific business logic, so socket applications generally have their own data formats, such as splicing together various parameters with delimiters (for example: file name | file), or using the common format xml or json etc. for easy parsing ( {"filename": "", "data": ""} ). At the same time, due to MTU limitations, a complete piece of business data may be unpacked and sent when sent, and packet loss may occur due to network reasons, so the "socket data length" parameter will be added to the custom data format, similar to The Content-Length of HTTP POST is generally added to the beginning of a piece of data to mark the length of the entire subsequent socket data. The "length" parameter does not participate in the data format. A certain subsection length agreed upon by both parties represents the "socket data length" parameter. When the receiver processes the data, it first parses the "length" of the first few digits, and then determines the subsequent complete data based on this length.

    If you encounter a problem during Socket development and the cause cannot be clearly determined, it is recommended to capture the packet and analyze it first to see how the data is transmitted, troubleshoot the network fault, and then determine whether it is a sender problem or a receiver problem. If there is no problem with the sender and receiver, and there is no problem with the service code, finally consider checking the reasons for the "network proxy", such as routers, operators, etc. It is also easy for problems to occur in the middle layer of China's network.

    reply
    0
  • Cancelreply