# Unpack, Determine which packet it belongs to Recv_data,(server_ip,server_port) = client_socket.recvfrom(1024) # The client receives data from the server, Packets or error Data packets Print(" Download the file :%s"%file_name)ĭownload(file_name,client_ip,client_port) # Get the filename, And change the byte into a stringįile_name = recv_code('utf-8') Operator_code = struct.unpack('!H',recv_data) # function unpack() Unpack the packet, After judging the seven characters, you can know whether it is a read-write request packet If int(operator_code) != 4 or int(ack_num) < 1: Print(" client :%s, Your confirmation message is "%client_ip,ack_num) Operator_code,ack_num = struct.unpack('!HH',recv_ack) Print(" client :%s, File download complete "%client_ip) ![]() If len(read_data) < 512: # The contents of the document have just been read New_ndto(data_package,(client_ip,client_port)) # The contents of the file are already byte data, Therefore, there is no need to put pack() inĭata_package = struct.pack('!HH',3,num) + read_data # When the file exists, Cut the content into packets and send them to the client, Every bag 512B New_ndto(error_package,(client_ip,client_port))Įxit() # After sending the error message, The current client exits the server # When there is an error opening the file, return ERROR Data packetsĮrror_package = struct.pack('!HH5sb',5,5,'error'.encode('utf-8'),0) Num = 1 # File content packet number counter # The server needs to open a new port, that is, create a new port socket, Send the file content to the client ![]() Download the code of the file from the serverĭef download(filename,client_ip,client_port): In the above code ,Python Medium struct It's used to deal with C Of structural data, The main formatting characters are as follows : Formatģ. #!H%dsb5sb Representation format, With ! start, Such as b Yes, it will Python Medium int Turn into C Medium charĬlient_ndto(data_package,host_port)Įnter the file name on the client side “JS.png” after, Server-side output : # Define the address and port of the target serverĭata_package = struct.pack('!H%dsb5sb'%len(file_name),1,file_name.encode('utf-8'),0,'octet'.encode('utf-8'),0) # Change data to C Byte stream recognized by language interpreterįile_name = input(' Please enter filename :')Ĭlient_socket = socket(AF_INET,SOCK_DGRAM) #struct be responsible for Python Data structure and C Conversion between data structures Print(recv_data,client_ip,client_port) # Print the received data Recv_data,(client_ip,client_port) = socket_server.recvfrom(1024) Socket_server.bind(('',69)) # binding IP And port number Socket_server = socket(AF_INET,SOCK_DGRAM) # Create socket Simple data transmission code between server and client In the picture above “ Pattern ” Refers to the format in which data is transmitted, It is generally used C The interpreter of language, use octet Pattern. Regulations, When the data received by the client is less than 516 byte (2 Byte opcode +2 Byte sequence number +512 Bytes of data ) when, It means that the server has finished sending. If the file to be downloaded does not exist, The server will send an error message, Is the file content or wrong information distributed to the area, And with 2 Bytes to represent the function of the packet ( It's called the opcode ), And in front of the serial number. When the file is large, The server divides the file into several packets, And will send more 2 Bytes of data are used to store serial numbers, And put it in front of the data, The serial number from 1 Start. ③ The server uses the new port to send files to the client, The client sends a message after successful acceptance ACK The reply. ② If the server allows the request, The server opens a new port for file transfer ① The client sends a download or upload request, Send the request to 69 port ![]() Ĭharacteristic : Simple 、 Small footprint 、 Suitable for transferring small files 、 It is suitable for transmitting data in LAN 、 The default port number is 69、 be based on UDP Realization. TFTP(Trival File Transfer Protocol, Simple file transfer protocol ) yes TCP/IP One of the protocol families is used for simple file transfer between client and server.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |