-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
65 lines (51 loc) · 1.79 KB
/
client.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <boost/asio.hpp>
#include <algorithm>
#include "json/single_include/nlohmann/json.hpp"
using boost::asio::ip::tcp;
using json = nlohmann::json;
using namespace std;
void send_request(const json& request) {
boost::asio::io_context io_context;
tcp::socket socket(io_context);
tcp::resolver resolver(io_context);
boost::asio::connect(socket, resolver.resolve("127.0.0.1", "2906"));
string request_data = request.dump() + '\0';
boost::asio::write(socket, boost::asio::buffer(request_data));
boost::asio::streambuf buffer;
boost::asio::read_until(socket, buffer, '\0');
istream is(&buffer);
string response_data;
getline(is, response_data, '\0');
json response = json::parse(response_data);
cout << "\nResponse: " << response.dump(4) << endl;
}
int main() {
string command;
while (true) {
cout << "Enter the command (read/write): ";
cin >> command;
transform(command.begin(), command.end(), command.begin(), ::tolower); // command to lower case
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (command == "read") {
string key;
cout << "Enter key: ";
cin >> key;
json request = {{"request", "read"}, {"key", key}};
send_request(request);
}
else if (command == "write") {
string key, value;
cout << "Enter key: ";
getline(cin, key);
cout << "Enter value: ";
getline(cin, value); // read value with spaces
json request = {{"request", "write"}, {"key", key}, {"value", value}};
send_request(request);
}
else {
cout << "Unknown command." << endl;
}
}
return 0;
}