-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
101 lines (85 loc) · 2.35 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include "zmq.h"
#include "converter.h"
using namespace std;
const size_t MESSAGE_SIZE = 512;
int main(void) {
bool first = true;
string bank_str = "";
size_t bank_id = 0;
do {
if (first) {
first = false;
} else {
cout << "Номер банка введён некорректно" << endl;
}
cout << "Введите номер банка:" << endl;
getline(cin, bank_str);
bank_id = StringToUNum(bank_str);
} while (!IsStringDouble(bank_str) || bank_id == 0);
cout << "Введите ip-адрес сервера [default `localhost`]:" << endl;
string ip_str = "";
getline(cin, ip_str);
ip_str = GetParameter(ip_str, 0);
if (ip_str == "") {
ip_str = "localhost";
}
void* context = zmq_ctx_new();
void* request = zmq_socket(context, ZMQ_REQ);
string address = "tcp://" + ip_str + ":";
address += UNumToString(bank_id);
if (zmq_connect(request, address.c_str()) != 0) {
cout << "Не удалось подключиться к серверу" << endl;
return 0;
}
first = true;
string id_str = "";
double user_id = 0.0;
do {
if (first) {
first = false;
} else {
cout << "Номер лицевого счёта введён некорректно" << endl;
}
cout << "Введите номер Вашего лицевого счёта:" << endl;
getline(cin, id_str);
user_id = stod(id_str);
} while (!IsStringDouble(id_str) || user_id == 0.0);
cout << "Настройка клиента закончена" << endl;
id_str += " ";
while (true) {
char str_tmp[MESSAGE_SIZE];
cout << ">>> ";
string cmd = "";
getline(cin, cmd);
//cout << "Point 0" << endl;
if (cmd == "" || cmd == "exit") {
if (cmd == "") {
cout << "exit" << endl;
}
break;
}
cmd = id_str + cmd;
zmq_msg_t req;
zmq_msg_init_size(&req, MESSAGE_SIZE);
strcpy(str_tmp, cmd.c_str());
memcpy(zmq_msg_data(&req), str_tmp, MESSAGE_SIZE);
zmq_msg_send(&req, request, 0);
zmq_msg_close(&req);
zmq_msg_t reply;
zmq_msg_init(&reply);
zmq_msg_recv(&reply, request, 0);
memcpy(&str_tmp, zmq_msg_data(&reply), MESSAGE_SIZE);
str_tmp[MESSAGE_SIZE - 1] = '\0';
cout << str_tmp << endl;
zmq_msg_close(&reply);
}
zmq_close(request);
zmq_ctx_destroy(context);
cout << "До свидания!" << endl;
return 0;
}