-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
156 lines (136 loc) · 3.83 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include <unistd.h>
#include <arpa/inet.h>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <strings.h>
#include <mutex>
#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define CLIENT_ID 0
std::mutex mtx;
struct sockaddr_in server_address;
void split(const std::string& s, std::vector<std::string>& parameters, const char delim = ' '){
parameters.clear();
std::istringstream iss(s);
std::string temp;
while (std::getline(iss, temp, delim)) {
parameters.emplace_back(std::move(temp));
}
return;
}
int write_request(std::string key, std::string value){
/*
send:
request type
client_id
the written key
the written value
receive:
OK
*/
int client_socket;
std::string response_message;
const uint32_t request_type = 1;
std::string reponse_message;
//create socket
if((client_socket = socket(AF_INET, SOCK_STREAM , 0)) < 0){
perror("Failed to create socket");
exit(0);
}
//connect to server
if(connect(client_socket,(struct sockaddr*) &server_address, sizeof(server_address)) < 0){
perror("Failed to connect");
exit(0);
}
//write (key,value)
int client_id = CLIENT_ID;
send(client_socket, &client_id, sizeof(client_id), 0);
send(client_socket, &request_type, sizeof(request_type), 0);
send(client_socket, &key, sizeof(key), 0);
send(client_socket, &value, sizeof(value), 0);
if(recv(client_socket, &response_message, sizeof(response_message), 0) < 0){
perror("server response failed");
return -1;
}
std::cout << "server response: " << response_message << std::endl;
close(client_socket);
return 0;
}
int read_request(std::string key){
/*
send:
request type
client_id
key
receive:
'found' if server found (key,value)
'no record' else
*/
int client_socket;
const uint32_t request_type = 0;
std::string response_message;
//create socket
if((client_socket = socket(AF_INET, SOCK_STREAM , 0)) < 0){
perror("Failed to create socket");
exit(0);
}
//connect to server
if(connect(client_socket,(struct sockaddr*) &server_address, sizeof(server_address)) < 0){
perror("Failed to connect");
exit(0);
}
//read request
int client_id = CLIENT_ID;
send(client_socket, &client_id, sizeof(client_id), 0);
send(client_socket, &request_type, sizeof(request_type), 0);
send(client_socket, &key, sizeof(key), 0);
//wait for response
if(recv(client_socket, &response_message, sizeof(response_message), 0) < 0){
perror("server response failed");
return -1;
}
std::cout << "server response: " << response_message << std::endl;
close(client_socket);
return 0;
}
int parse_command(std::string command){
std::vector<std::string> parameters;
split(command, parameters);
if(parameters[0] == "read"){
read_request(parameters[1]);
}
else if(parameters[0] == "write"){
write_request(parameters[1], parameters[2]);
}
return 0;
}
void user_interface(){
char buffer[1024];
while(1){
std::string command;
std::cout<<">";
std::cin.getline(buffer, 1024);
command = buffer;
parse_command(command);
}
return;
}
int main(void){
//initailize address structure
bzero(&server_address, sizeof(server_address));
server_address.sin_family = PF_INET;
server_address.sin_addr.s_addr = inet_addr(SERVER_IP);
server_address.sin_port = htons(PORT);
user_interface();
return 0;
}