-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
78 lines (67 loc) · 2.05 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
66
67
68
69
70
71
72
73
74
75
76
77
78
//
// Created by JakoError on 2022/10/20.
//
#include "client.hpp"
#include <boost/regex.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <iostream>
#include <string>
#include <list>
#include <boost/algorithm/string/trim.hpp>
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
using std::string;
namespace cppDBMS {
void DBMSClient::connect() {
for (int i = 0; i < connect_try_times; ++i) {
try {
// ep = ip::tcp::endpoint(ip::address::from_string(ip_address), port);
sock = socket_ptr(new ip::tcp::socket(ios));
sock->connect(ep);
} catch (std::exception const &x) {
std::cerr << boost::diagnostic_information(x) << std::endl;
std::flush(std::cerr);
system("pause");
continue;
}
cout << "connected to server:" << ip_address << " port:" << port << endl;
break;
}
}
void DBMSClient::start() {
char result[MAX_CMD_SIZE] = {};
try {
while (true) {
if (!sock->is_open())
connect();
cout << cmd_line;
string cmd;
getline(cin, cmd);
boost::trim(cmd);
if (cmd.length() == 0)
continue;
if (cmd == "exit")
break;
write(*sock, buffer(cmd));
memset(result, 0, MAX_CMD_SIZE);
size_t len = sock->read_some(buffer(result));
if (len > 0)
cout << string(result) << endl;
else
cout << "io read failed" << endl;
}
} catch (std::exception const &x) {
std::cerr << boost::diagnostic_information(x) << std::endl;
std::flush(std::cerr);
system("pause");
}
sock->close();
}
}
int main() {
cppDBMS::DBMSClient client("127.0.0.1", 8000);
client.connect();
client.start();
}