-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_recv_file.cpp
37 lines (29 loc) · 977 Bytes
/
client_recv_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
#include <iostream>
#include <fstream>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
void start_receiving(tcp::socket& socket, const std::string& file_name) {
std::ofstream file(file_name, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open the file.\n";
return;
}
boost::asio::streambuf buf;
boost::asio::read(socket, buf);
std::istream in_stream(&buf);
file << in_stream.rdbuf();
}
int main() {
try {
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query("localhost", "1234");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::socket socket(io_service);
boost::asio::connect(socket, endpoint_iterator);
start_receiving(socket, "received_file.txt");
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}