Remote directory browsing and multi-threaded file downloading based on select I/O model

Source: Internet
Author: User
Tags file size file system ini thread

Summary:

The server side is based on the select I/O model. To prevent the program interface from blocking, a child thread is used to continuously receive the socket and select its processing. The client has only one thread function, but its utilization is high, it can be used for remote directory exchange, request file size, and create several threads to download files. File transfer has upload and download, as well as peer-to-peer transmission, in this project, the transmission file specifically refers to the download.

Body:

A data and structure

1 transfer packet, client main thread pass to the child of the structure body

typedef struct{
    char packetType; // 请求类型 r:request rootdriver d:directory f:file D::data
    unsigned int length; //用于传送int类型
    char content[2000]; //传送的内容
    }DATA_PACKET;

Request type: ' R ': Server logical drive letter, D; folder and file name under letter and folder, F ': File size, D: File contents.

At the client request download file, the end of length two bits are used to record the total number of downloaded threads, preceded by a record of the current line program number. This inverse operation is easy to implement on the server side.

The content is used to hold the absolute path of the request file when the file size and request download are requested. This counter operation on the server side is handled using a custom function.

2 Global variables (for communication between threads)

char* pDrives; // 数据缓冲
CString arrFiles[200]; //文件目录字符串数组
CString savePath; //文件保存路径
long fSize=0l, ,recvFSize=0l; //文件大小,已经接收的文件大小
CString strIP; //IP
bool thrFlag=false; //下载线程创建置位

In theory, you should avoid using global variables as much as possible in your program, because you don't see Java and C # fully oriented classes because of the destruction of the program structure. For convenience, however, global variables are used and are application-level, so that communication between threads is convenient. It is worth mentioning that here Thrflag is important, it involves ensuring that threads create a certain correct problem, in the later "to pay attention to a few issues" will be discussed in more detail.

3 Directory tree Class Cextntreectrl::ctreectrl

This class inherits from the CTreeCtrl class, and the main extension obtains the entire path of an item in the tree, so that the path can be normalized to request a file from the server. A function that converts a full path to a filename is also implemented here.

For how to construct this tree, there is a very convenient and efficient way: Double-click the tree, double-click the item as the root. If the letter or folder, send the request to the server, the server will be returned, automatically populated under this key. If it is a file, the item name has an extension and the download is requested. This approach is easy to operate, but also improves system performance, at least in the local area network. It would be much slower if you requested the contents of the entire file system from the server at once.

4. parameter setup dialog box class Csetparam

This class is used to set the number of threads to download and the default Save folder path. This class will write a "Setting.ini" file on the C disk to hold the parameters. Specifically, if the user has not set these parameters, then getprivateprofilestring (...) Attempting to read the "C:\Setting.ini" file returns the default number of threads 0, and a string "DefaultPath" that is not a path, at which point the download automatically sets the parameters, 3, "C:\."

Two problems to pay attention to

1 MFC vs. Windows APIs

In the case of multithreaded programming, if you use MFC's CAsyncSocket or CSocket, the main thread to the line Cheng parameters is a very headache, if the use of Windows API to achieve more flexible. In the file operation aspect, the CFile is also good. Still, I used the Windows API function for the design to meet the requirements.

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.