-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
323 lines (280 loc) · 8.21 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include "opencv2/opencv.hpp"
#define BUFF_SIZE 1024
using namespace std;
using namespace cv;
void *child(void* ptr);
Mat imgClient[128];
int localSocket, start, end;
int videofinish;
int main(int argc , char *argv[])
{
int recved, port = atoi(argv[1]);
localSocket = socket(AF_INET , SOCK_STREAM , 0);
if (localSocket == -1){
printf("Fail to create a socket.\n");
return 0;
}
struct sockaddr_in info;
bzero(&info,sizeof(info));
info.sin_family = PF_INET;
info.sin_addr.s_addr = inet_addr("127.0.0.1");
info.sin_port = htons(port);
int err = connect(localSocket,(struct sockaddr *)&info,sizeof(info));
if(err==-1){
printf("Connection error\n");
return 0;
}
struct stat sv = { 0 };
if (stat("/clientdir", &sv) == -1) { //mkdir
mkdir("./clientdir", 0777);
}
char receiveMessage[BUFF_SIZE] = {};
char buf[BUFF_SIZE] = {};
char cmd[BUFF_SIZE], filename[BUFF_SIZE], path[BUFF_SIZE];
int sent;
while(1){
// get cmd from stdin
bzero(buf, sizeof(char) * BUFF_SIZE);
fgets(cmd, BUFF_SIZE, stdin);
cmd[strlen(cmd)-1] = '\0';
strcpy(buf, cmd);
// ls
if (strcmp(cmd, "ls") == 0) {
// send cmd to server
sent = send(localSocket, buf, strlen(buf), 0);
bzero(receiveMessage, sizeof(char) * BUFF_SIZE);
int sz;
while (1) {
// get size of ls chunk
if ((recved = recv(localSocket, &sz, sizeof(int), 0)) < 0) { // wrong recv
cout << "recv failed, with received bytes = " << recved << endl;
break;
}
else {
if (sz < 0) { break; } // finish
// receive ls chunk
if ((recved = recv(localSocket, receiveMessage, sz+1, MSG_WAITALL)) < 0) {
cout << "recv failed, with received bytes = " << recved << endl;
break;
}
else if (recved == 0) {
cout << "<end>\n";
break;
}
else {
receiveMessage[sz] = '\0';
printf("%s", receiveMessage);
}
}
}
}
// put
else if (strncmp(cmd, "put", 3) == 0) {
if (cmd[3] != ' ') { printf("Command format error.\n"); }
else {
// get file path
for (int j = 4; j < strlen(cmd); j++) {
filename[j - 4] = cmd[j];
}
filename[strlen(cmd) - 4] = '\0';
strcpy(path, "./clientdir/");
strcat(path, filename);
// open file
int fd = open(path, O_RDONLY);
if (fd < 0) { // check the file
printf("The %s doesn't exist.\n", filename);
}
else {
// send cmd
sent = send(localSocket, buf, sizeof(char) * BUFF_SIZE, 0);
// send file size
struct stat st;
fstat(fd, &st);
int sz = st.st_size;
sent = send(localSocket, (char*)& sz, sizeof(int), 0);
// send file
while (read(fd, buf, sizeof(char) * BUFF_SIZE)) {
sent = send(localSocket, buf, 1024, 0);
}
close(fd);
}
}
}
// get
else if (strncmp(cmd, "get", 3) == 0) {
if (cmd[3] != ' ') { printf("Command format error.\n"); }
else {
// send cmd
sent = send(localSocket, buf, sizeof(char) * BUFF_SIZE, 0);
// get filename
for (int j = 4; j < strlen(cmd); j++) {
filename[j - 4] = cmd[j];
}
filename[strlen(cmd) - 4] = '\0';
// get file size
int sz;
bzero(receiveMessage, sizeof(char)* BUFF_SIZE);
if ((recved = recv(localSocket, (char*)&sz, sizeof(int), 0)) < 0) { // wrong recv
cout << "recv failed, with received bytes = " << recved << endl;
break;
}
else {
if (sz < 0) { printf("The %s doesn't exist.\n", filename); }
else {
// get file path
strcpy(path, "./clientdir/");
strcat(path, filename);
// create file
int fd = open(path, O_WRONLY | O_CREAT);
if (fd < 0) {
printf("open %s error.\n", filename);
}
else {
//get file
while (sz > 0) {
if ((recved = recv(localSocket, receiveMessage, sizeof(char) * BUFF_SIZE,MSG_WAITALL)) < 0) {
cout << "recv failed, with received bytes = " << recved << endl;
break;
}
else if (recved == 0) {
cout << "<end>\n";
break;
}
else {
if (sz < BUFF_SIZE) { //last chunk
receiveMessage[sz] = '\0';
write(fd, receiveMessage, sz);
sz = 0;
}
else { // other chunks
write(fd, receiveMessage, 1024);
sz -= 1024;
}
}
}
printf("getfinish.\n");
close(fd);
}
}
}
}
}
// play
else if (strncmp(cmd, "play", 4) == 0) {
if (cmd[4] != ' ') { printf("Command format error.\n"); }
else {
// get filename
for (int j = 5; j < strlen(buf); j++) {
filename[j - 5] = buf[j];
}
filename[strlen(buf) - 5] = '\0';
// check if it is .mpg
int chk = 0, flen = strlen(filename);
if (filename[flen - 4] != '.') { chk = 1; }
if (filename[flen - 3] != 'm') { chk = 1; }
if (filename[flen - 2] != 'p') { chk = 1; }
if (filename[flen - 1] != 'g') { chk = 1; }
if (chk) { printf("The %s is not a mpg file.\n", filename); }
else {
// send cmd
sent = send(localSocket, buf, strlen(buf), 0);
bzero(receiveMessage, sizeof(char) * BUFF_SIZE);
// receive width and height
int width=0, height=0;
if ((recved = recv(localSocket, &width, sizeof(int), MSG_WAITALL)) < 0) { // wrong recv
cout << "width recv failed, with received bytes = " << recved << endl;
break;
}
if (width == -1) { // file doesn't exist
printf("The %s doesn't exist.\n", filename);
}
else {
if ((recved = recv(localSocket, &height, sizeof(int), MSG_WAITALL)) < 0) { // wrong recv
cout << "height recv failed, with received bytes = " << recved << endl;
break;
}
for (int k = 0; k < 128; k++) {
imgClient[k] = Mat::zeros(height, width, CV_8UC3);
//allocate container to load frames
// ensure the memory is continuous (for efficiency issue.)
if (!imgClient[k].isContinuous()) {
imgClient[k] = imgClient[k].clone();
}
}
start = 0;
end = -1;
pthread_t pid;
pthread_create(&pid, NULL, child, NULL);
videofinish = 0;
while (1) {
if (start != end && end != -1) {
// show frame from brffer
imshow("Video", imgClient[start]);
start = (start + 1) % 128;
if (videofinish && start == end) { imshow("Video", imgClient[start]); break; }
//Press ESC on keyboard to exit
// notice: this part is necessary due to openCV's design.
// waitKey means a delay to get the next frame.
char c = (char)waitKey(33.3333);
if (c == 27) { //send to server?
int esc = -1;
sent = send(localSocket, &esc, sizeof(int), 0);
break;
}
}
}
pthread_join(pid, NULL);
destroyAllWindows();
}
}
}
}
// else
else {
printf("Command not found.\n");
}
}
printf("close Socket\n");
close(localSocket);
return 0;
}
void *child(void *ptr) {
while (1) {
if (end == -1 || (end+1)%128 != start) {
end = (end + 1) % 128;
int recved;
// get imgSize
int imgSize;
if ((recved = recv(localSocket, &imgSize, sizeof(int), MSG_WAITALL)) < 0) { // wrong recv
cout << "imgSize recv failed, with received bytes = " << recved << endl;
break;
}
// finish
if (imgSize == -1) { videofinish = 1; break; }
//if (imgSize != 1555200) { printf("imgSize = %d\n", imgSize); }
// allocate a buffer to load the frame (there would be 2 buffers in the world of the Internet)
uchar buffer[imgSize];
if ((recved = recv(localSocket, buffer, sizeof(unsigned char) * imgSize, MSG_WAITALL)) < 0) { // wrong recv
cout << "imgSize recv failed, with received bytes = " << recved << endl;
break;
}
// copy a fream from buffer to the container on client
uchar* iptr = imgClient[end].data;
memcpy(iptr, buffer, imgSize);
}
}
pthread_exit(NULL);
}