c++ 本地http服务与客户端实现文件传输代码
时间: 2023-09-30 20:03:30 浏览: 203
C++实现文件传输代码
这里给出一个基于 Boost.Asio 库的 C++ 实现的简单 HTTP 服务器和客户端,可以实现文件传输的功能。
HTTP 服务器代码:
```c++
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
using namespace boost::asio;
using namespace boost::asio::ip;
const std::string SERVER_ADDR = "127.0.0.1";
const int SERVER_PORT = 8080;
const std::string FILE_PATH = "test.txt";
void handle_request(tcp::socket& socket) {
boost::array<char, 8192> buf;
boost::system::error_code error;
size_t len = socket.read_some(buffer(buf), error);
if (error == boost::asio::error::eof) {
return;
} else if (error) {
throw boost::system::system_error(error);
}
std::string request(buf.data(), len);
std::string response;
// parse request and prepare response
std::stringstream ss(request);
std::string method, path, version;
ss >> method >> path >> version;
std::cout << method << " " << path << " " << version << std::endl;
std::ifstream file(FILE_PATH, std::ios::binary);
if (file) {
file.seekg(0, std::ios::end);
size_t file_size = file.tellg();
file.seekg(0, std::ios::beg);
response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: application/octet-stream\r\n";
response += "Content-Length: " + std::to_string(file_size) + "\r\n";
response += "\r\n";
// send response header
write(socket, buffer(response), error);
if (error) {
throw boost::system::system_error(error);
}
// send file content
char file_buf[8192];
while (file.read(file_buf, sizeof(file_buf)).gcount() > 0) {
write(socket, buffer(file_buf, file.gcount()), error);
if (error) {
throw boost::system::system_error(error);
}
}
} else {
response = "HTTP/1.1 404 Not Found\r\n";
response += "\r\n";
write(socket, buffer(response), error);
if (error) {
throw boost::system::system_error(error);
}
}
}
int main() {
io_service service;
tcp::endpoint ep(address::from_string(SERVER_ADDR), SERVER_PORT);
tcp::acceptor acceptor(service, ep);
while (true) {
tcp::socket socket(service);
acceptor.accept(socket);
std::thread t([&socket]() { handle_request(socket); });
t.detach();
}
}
```
HTTP 客户端代码:
```c++
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
using namespace boost::asio;
using namespace boost::asio::ip;
const std::string SERVER_ADDR = "127.0.0.1";
const int SERVER_PORT = 8080;
const std::string FILE_PATH = "test_download.txt";
void download_file(tcp::socket& socket) {
std::ofstream file(FILE_PATH, std::ios::binary);
if (!file) {
throw std::runtime_error("Failed to open file: " + FILE_PATH);
}
// send HTTP request
std::string request = "GET / HTTP/1.1\r\n";
request += "Host: " + SERVER_ADDR + ":" + std::to_string(SERVER_PORT) + "\r\n";
request += "Connection: close\r\n";
request += "\r\n";
write(socket, buffer(request));
// read HTTP response
boost::array<char, 8192> buf;
boost::system::error_code error;
size_t len = read_until(socket, buffer(buf), "\r\n\r\n", error);
if (error) {
throw boost::system::system_error(error);
}
std::string response(buf.data(), len);
std::cout << response << std::endl;
// parse response header
size_t pos = response.find("Content-Length: ");
if (pos == std::string::npos) {
throw std::runtime_error("Invalid response header format: " + response);
}
size_t file_size = std::stoull(response.substr(pos + 16));
// read file content
size_t total_read = 0;
while (total_read < file_size) {
size_t read_size = std::min<size_t>(file_size - total_read, sizeof(buf));
len = read(socket, buffer(buf, read_size), error);
if (error) {
throw boost::system::system_error(error);
}
file.write(buf.data(), len);
total_read += len;
}
std::cout << "File download finished." << std::endl;
}
int main() {
io_service service;
tcp::endpoint ep(address::from_string(SERVER_ADDR), SERVER_PORT);
tcp::socket socket(service);
socket.connect(ep);
download_file(socket);
socket.close();
}
```
这里只是一个简单的示例,没有考虑安全性和可靠性等问题,仅供参考。
阅读全文