-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
219 lines (200 loc) · 5.93 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Client side C/C++ program to demonstrate Socket programming
// Project #1 :: RealTime ::<< endl
// Project #1 :: RealTime ::<< endl
// Project #1 :: RealTime ::<< endl
// Project #1 :: RealTime ::<< endl
// Project #1 :: RealTime ::<< endl
// cout << Yahya Shqair :: 1160699 :: << Firas maali ::
// cout << Yahya Shqair :: 1160699 :: << Firas maali ::
// cout << Yahya Shqair :: 1160699 :: << Firas maali ::
// cout << Yahya Shqair :: 1160699 :: << Firas maali ::
#include <bits/stdc++.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define lp(i, n) for (int i = 0; i < (n) ; ++i)
#define lpp(i, n) for (int i = 1; i <= (n) ; ++i)
#define lpi(i, j, n) for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n) for(int i=(j);i>=(int)(n);--i)
#define sz(v) (int)(v).size()
#define clr(v, d) memset(v, d, sizeof(v))
#define mod 100000007
#define PORT 8000
// Client id and msg ID :
int cID , msgID;
int makeRequest();
void lock() {
char type[] = "1:";
char buffer[1024] = {0};
// open new socket for this request
int sock = makeRequest();
int ready = 0 ;// flag is true or false .
cout << "Wait for lock"<<endl;
send(sock , type , strlen(type) , 0 );
read( sock , buffer, 1024);
ready = atoi(buffer);
cout << "Locked successfully"<<endl;
}
void unlock() {
char type[] = "2:";
char buffer[1024] = {0};
int sock = makeRequest();
int ready = 0 ;
// loop is not important but
send(sock , type , strlen(type) , 0 );
read( sock , buffer, 1024);
ready = atoi(buffer);
if(ready != 1){
cout << " you must lock before unlock "<<endl;
return ;
}
cout << "Unlocked Successfully " << endl;
}
void remove() {
char type[] = "6:";
char buffer[1024] = {0};
int sock = makeRequest();
int ready = 0 ;
// loop is not important but
send(sock , type , strlen(type) , 0 );
read( sock , buffer, 1024);
ready = atoi(buffer);
if(ready != 1){
cout << " Fail "<<endl;
return ;
}
cout << "Removed Successfully " << endl;
}
void read() {
char type[] = "3:";
char buffer[1024];
buffer[0] = '0';
int sock = makeRequest();
int ready = 0 ;
send(sock , type , strlen(type) , 0 );
read( sock , buffer, 1024);
ready = atoi(buffer);
if (buffer[0] == '0') {
cout << "You must lock before read " << endl;
return ;
}
cout << buffer << endl;
cout << "Read Successfully " << endl;
}
void write() {
char type[] = "4:";
char buffer[1024];
buffer[0] = '0';
// open new Socket
int sock = makeRequest();
int ready = 0 ;
send(sock , type , strlen(type) , 0 );
read( sock , buffer, 1024);
ready = atoi(buffer);
if (buffer[0] == '0') {
cout << "You must lock before read " << endl;
return ;
}
// Read Data
cout << "Print the msg you want to sent ,, enter # to stop reading" << endl;
string x="",y;
while(cin >> y &&y[0]!='#'){
x+=y+" ";
}
// Move Date from std:string to char[] ;
lp(i, x.size()) {buffer[i] = x[i]; buffer[i + 1] = '\0';}
// send the buffer
send(sock , buffer , strlen(buffer) , 0 );
read( sock , buffer, 1024);
// if response = 1 is ture else failed ;
if (buffer[0] == '1') {
cout << "written Successfully " << endl;
} else {
cout << "Failed " << endl;
}
}
int makeRequest() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char hello[1100];
// each request has the first tcp msg is like 'web cookies'
// to tell the server client id and msg id .
// implement the cookie
string helloShakehand = std::to_string(cID) + ":" + std::to_string(msgID) + ":";
lp(i, helloShakehand.size()) {hello[i] = helloShakehand[i]; hello[i + 1] = '\0';}
cout << helloShakehand << endl;
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
read( sock , buffer, 1024);
if (atoi(buffer) != 1) {
cout << "error in connection" << endl;
return 0 ;
} else {
cout << "Connected ..." << endl;
return sock ;
}
return sock ;
}
int main()
{
while (1) {
readId :
cout << "Please Enter Client ID : ";
cin >> cID ;
readMID:
cout << endl << "Please Enter sheard Memory you want to access : ";
cin >> msgID ;
while (1) {
cout << endl << "Enter type of you Request : " << endl;
cout << "1- Lock " << endl;
cout << "2- unLock " << endl;
cout << "3- Read " << endl;
cout << "4- Write " << endl;
cout << "5- Change msg id " << endl;
cout << "6- Change Client session " << endl;
cout << "7- Remove me from msg client list " << endl;
int type ; cin >> type ;
if (type == 1) {
lock();
} else if (type == 2 ) {
unlock();
} else if (type == 3) {
read();
} else if (type == 4 ) {
write();
} else if (type == 5) {
goto readMID;
} else if (type == 6 ) {
goto readId;
}else if(type == 7){
remove();
goto readMID ;
} else {
cout << "this option not implemeted yet ." << endl;
}
}
}
return 0;
}