-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_send_file.cpp
41 lines (32 loc) · 1000 Bytes
/
server_send_file.cpp
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
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
void start_sending(tcp::socket& socket, const std::string& file_name) {
std::ifstream file(file_name, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open the file.\n";
return;
}
file.seekg(0, std::ios::end);
size_t file_size = file.tellg();
file.seekg(0, std::ios::beg);
boost::asio::streambuf buf;
std::ostream out_stream(&buf);
out_stream << file.rdbuf();
boost::asio::write(socket, buf);
}
int main() {
try {
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 1234));
for (;;) {
tcp::socket socket(io_service);
acceptor.accept(socket);
start_sending(socket, "file_to_send.txt");
}
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}