-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmp2
44 lines (34 loc) · 1.28 KB
/
tmp2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
// Function to calculate file's MD5 hash... (Your implementation here)
void send_file(tcp::socket& socket, const std::string& file_name) {
std::ifstream file(file_name, std::ios::binary);
// Check if the file was opened successfully.
if (!file.is_open() || file.fail()) {
std::cerr << "Failed to open the file.\n";
return;
}
// Get the file size.
file.seekg(0, std::ios::end);
std::streamsize size = file.tellg();
file.seekg(0, std::ios::beg);
// Read file into memory.
std::vector<char> file_data(size);
if (!file.read(file_data.data(), size)) {
std::cerr << "Failed to read the file.\n";
return;
}
// Calculate the MD5 of the file.
std::string md5 = get_file_md5(file_name);
std::cout << "MD5 of the file to be sent: " << md5 << std::endl;
std::cout << "Size of the file to be sent: " << size << " bytes\n";
// Send the MD5 to the client.
boost::asio::write(socket, boost::asio::buffer(md5));
// Then send the file data.
std::size_t written = boost::asio::write(socket, boost::asio::buffer(file_data));
std::cout << "Written bytes: " << written << "\n";
}