Home > OS >  Mode for the client Socket network disk file, upload and download
Mode for the client Socket network disk file, upload and download

Time:05-27


The server is written in Delphi do interface and code, the client just response, using c + + write a client,

download file:

(because of my code is to use the data flow and asynchronous messaging, post code, too much, here I'll use oral + capture important code);

1, after the connection is established, first get the client's disk information, here we give the client sends a command, such as "PKT_DISK" and similar message, the client accept the message, judgment is to accept is equal to the command, we related function or operation on the client side;

Here the function [GetLogicalDriveStrings] computer disk information; Then send the access to the disk information to the server;



2, on the server after recv to customer computer disk information, added to the interface, the client's disk has enumerated;



3, access disk directory under, also send a specific command and want to download a path to the client, the client after recv to the corresponding command, response code, using the function [FindFirstFile] [FindNextFile] to find related files of the path, you need to define a WIN32_FIND_DATA structure, returned to the structure, the judgment of the structure of the dwFileAttributes attribute to ". "and".. ", loop through the path to the file, and then one by one the file name, attribute, time, size sent to the server, the last call [FindClose] to close the handle to open;



4, the service side after receiving the information such as file name, to enumerate all the files in the directory,



After 5, there is the name of the file, we can follow download orders, send the client to download the order, will want to download the path name to the client, the client receives the message, using the function CreateFile received open file path, to judge whether a can normal open file, after the success of the open, using the function [GetFileTime] [GetFileAttributes] [GetFileSizeEx] to get time, file attributes, size, etc., after the success, send to the server,



6, when the client determine can be opened, the service side successful receive the news, read the client sends the file properties, size, time, using the function CreateFile to create a file, to accept to download data; Create success, to the client sends a message, tell the client, you can send me the need to download the file data;



7, the client calls the function [ReadFile] read just open the file, the circular sent to the server, until the number of bytes read is zero, the function call CloseHandle to close the handle, handle to prevent leakage;



8, the service side loop recv the client data, using the function [WriteFile] will receive data written to the newly created file, until the end of the received message is, exit the loop; After receiving, the use of function [SetFileAttributes] [setfiletime] Settings file attribute and time, use [CloseHandle] close the handle;

At this point, download a file success;



upload file:

1, the server using the function [CreatrFIle] want to upload the file open, use function [GetFileTime] [GetFileAttributes] [GetFileSizeEx] get time, the file attributes, size; The file information sent to the client; Waiting to receive the client whether can create the file command;




2, the client receives the message, using the function CreateFile create a file, create a successful, after sending a message to the server, create a file successfully, can send me need to upload data;



3, when the server receives the client after the success of the create a file, using the function [ReadFile] read just open the file, one by one to send data to the client, until the number of bytes to read the file to 0, then sends a command to the client to do it, and use the function [CloseHandle] close the handle;



4, the client open loop mode, has been receiving server sends the data, then use function [WriteFile] to the newly created file is written to the data of success, until received command is complete, to the server to send a command, tell the server receives the data upload success, using the function [SetFileTime] sets the file time, the final step in using the function [CloseHandle] [SetFileAttributes] to close the handle and set properties file,



5, the server side to perform again recv, after receiving the success, and the absence of errors, so far, upload a file is complete;





The server and client to a single file uploads and downloads will share here;

If you want to download/upload multiple files, add a variable at both ends, is used to record the number of need to download, circulation download/upload files;

If you want to download a folder, I adopt the method is relatively simple recursive algorithm, enumerated all the file folder, if there are any folders recursively again, most again calls to functions that create folders can upload/download folder.


welcome to explore, more guidance, a lot of communication;
  • Related