-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
59 lines (48 loc) · 1.17 KB
/
client.cpp
File metadata and controls
59 lines (48 loc) · 1.17 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "Client.hpp"
#include <exception>
#include <iostream>
#include <array>
#include <string>
Client::Client(std::string host, std::string port)
{
tcp::resolver resolver(io_context);
endpoints = resolver.resolve(host, port);
}
std::string Client::connect(const std::string& data)
{
std::string response;
if (data.size() < 4)
{
std::cerr << "You must send at least 4 bytes of data\n";
return "NULL";
}
try
{
/* Open a socket for sending data */
tcp::socket socket(io_context);
boost::asio::connect(socket, endpoints);
/* Send data to the server */
boost::asio::write(socket, boost::asio::buffer(data), ignored_error);
socket.close();
/* Open a new socket for receiving data */
boost::asio::connect(socket, endpoints);
/* Wait for a response */
while (true)
{
std::array<char, 128> buf;
boost::system::error_code error;
size_t len = socket.read_some(boost::asio::buffer(buf), error);
if (error == boost::asio::error::eof)
break;
else if (error)
throw boost::system::system_error(error);
response += buf.data();
}
socket.close();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return response;
}