Socketserver's recv (1024) Problem!

Source: Internet
Author: User



first, the socket sends the data basic flow
    • Icon
      • Process explanation
        • The above process is the basic process of receiving and sending data, and using ACK to solve the problem of sticky packet ,
        • Finally, the user-friendly display is given by obtaining the "Get file Success" sent by the server.

second, practical application problems:
    • Problem performance:
      • The client has received all the file data, but it is still blocked , that is: "Get file Success" is not received
    • Troubleshoot the problem:
      • View the client side received the file, found "Get file Success" This message, was appended to the end of the file!
      • To view the client-side main code:
with open (file, ' WB ') as F1:
recv_size = 0
While
Recv_size < file_size:
data = SELF.SK.RECV (1024x768)
Recv_size + = len (data)
f1.write (data)

Print (SELF.SK.RECV (1024x768). Decode ()) # Printing friendly information
print (' send_file:{} md5_sum:{} '. Format (file_size, file_sum), ' recv_file:{} md5_sum:{} '. Format (recv_size, md5_ sum), sep= ' \ n ') # Print file MD5 value
    • Summary of Causes:
      • Because the client is accepting data through SK.RECV (1024), and 1024 means that it accepts up to 1024 bytes at a time ,
      • When all the data in the file itself is less than 1024, then the last "Get file success" friendly information will be accepted by the client one time, as shown in data = SELF.SK.RECV (1024)
      • i.e. at this point:
        • Data includes all of the information in the file itself + "friendly" data,
        • And when the code goes to print (SELF.SK.RECV (1024x768). Decode ()), it blocks (in fact, the server has sent the friendly information, just because 1024 of the reason, as the file itself data)
    • How to resolve:
      • 1, change 1024 for smaller units, such as recv (10)
        • Receive more, and if it is a file, there must be a number (file size/ten), such as the last remaining 3 bytes are not accepted, and 3< 10, so these 3 bytes will be mixed with "friendly information"
      • 2. Do not send friendly information
        • In the client side directly judge Recv_data_size and Send_data_size, and calculate the file hash value, to determine whether the file is complete,
        • This should be done by deleting the interactive code on "friendly Information" at both ends












Socketserver's recv (1024) Problem!

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.