[Actual] --- P2P downloader project

1 Introduction

Often need to learn during the school committee, the squad leader to copy files, pass each job information, etc., if you put aside some chat tools, how to host each other online file-sharing it? With all the confusion, understanding of network transmission and Socket socket programming, after the relevant technical HTTP protocol format, CPP-httplib, block transmission mechanism, slowly groping, trial and error, and thus with this project.

2. Project

Environment : Windows10 VS2013 C / C ++

  • The project was completed in aNearby LAN file sharing downloadFunction tool
  1. A host can run a search tool to match the LAN; local area network to get online list of hosts;
  2. You can obtain a list of information (files in the specified shared directory) file specifies the host shared;
  3. Multi-block process can download the specified file on the specified host to improve transmission efficiency.

3. The overall framework

Here Insert Picture Description
Here Insert Picture Description

4. The server design

The server-side design:

Function : Design and Implementation of HTTP server program that provides a browser client to download files, access to the file list

Server process

①. To build HTTP server

  • 1. Host processing pairing request
  • 2. Host file list acquisition processing
  • 3. Host acquiring data acquisition function

②. Provided near the host pairing

③. Provide a list of files to nearby host

④. To provide file download function to nearby host

5. Client Design

Function : HTTP server to achieve the multi-function block transmission process block file download manager functions, improve the efficiency of the transmission block-based transfer

Client Process

  • 1. Discover nearby local area network users to share, get all the information in the LAN IP address
  • 2. Send the host pairing request to obtain the IP address of the host - Get a list of IP addresses to host a successful match, pairing successful print a list of hosts
  • 3. Select the user to share files where you want to get a host of
  • 4. Get the list to the designated host sends a file request - to obtain a list of shared files on the host
  • 5. Print a list of all files, get file header information, the length of the main document acquisition, acquisition request to the specified host file data transmission
  • 6. If the file is too large, then create a multi-thread download chunked transfer.

6. The main function of the port

Client :

  1. Provide the host pairing clients
bool GetonlineHost()
  1. Provide the client file list acquisition function
bool GetShareList(const std::string &host_ip)
  1. Provide the client file download function (Normal download block transmission of the download &&)
//普通下载
bool DownloadFile(const std::string &host_ip, const std::string& filename)
//分块传输下载
bool RangeDownload(const std::string &host_ip, const std::string &filename)

Server :

  1. Provide matching function to discover nearby host LAN
static void HostPair(const httplib::Request &req, httplib::Response &rsp)
  1. Gets the specified hosts can provide shared file list function
	//获取共享文件列表,在主机上设置一个共享目录,凡是这个目录下的文件都是要给别人共享的
	static void ShareList(const httplib::Request &req, httplib::Response &rsp)
  1. Provide a download under specified specified hosts file sharing function
static void Download(const httplib::Request &req, httplib::Response &rsp)

Other interfaces :

httplib basic use:
1. Examples of the client object

httplib::Client cli(buf, P2P_PORT); //实例化httplib客户端对象

2. Examples of server objects

httplib::Server _srv;

Under Windows acquiring card information

static bool  GetAllAdapter(std::vector<Adapter> *list)

7.httplib process flow:

Build client:
1. Organize HTTP request data protocol format
2. build TCP client
3 transmits the organized HTTP request data
4. Wait for the response to the server, the server receives data
5. parsing response data

Build server:
1. build a simple TCP server
2 waits for receiving data sent by the client
3. The HTTP protocol format, parses the data (URL request process is performed as protocol version)
according to the resource request and the query string path text, a service description
in response to tissue 5. HTTP protocol format, returned to the client (status code description protocol version)

8. Source

Click to view the source code (https://github.com/SJRLL/gongxiangxiazaiqi)

Published 57 original articles · won praise 301 · views 40000 +

Guess you like

Origin blog.csdn.net/L19002S/article/details/104942981