first, the socket sends the data basic flow
- 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!