-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
384 lines (306 loc) · 12.2 KB
/
Copy pathserver.cpp
File metadata and controls
384 lines (306 loc) · 12.2 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <iostream>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <fstream>
#include <sys/time.h>
#include <thread>
#include <filesystem>
#include <csignal>
#include <atomic>
#include <ctime>
#include <mutex>
#define SERVER_PORT 6000
#define maxBufferLength 4096
int handleClient(int);
int clientUpload(int, const char[], int);
int handleUpload(int);
void createOutputFolder();
void signalHandler(int);
int handleDownload(int);
void log(const std::string&);
// Structure declaration
struct FileHeader{
char fn[128]; // File's name
int fs; // File's size
};
// Initialize serverSocket to -1 (to safely handle handle in the signalHangler() function)
int serverSocket = -1;
// create an atomic flag to keep the server running in main
// This flag should be atomic because "Ctrl + C" could be pressed at any moment
std::atomic<bool> keepRunning(true);
// Declare logFile
std::ofstream logFile("logServer.txt", std::ios::app);
std::mutex logMutex;
int main(){
int clientSocket;
struct sockaddr_in serverAddress, clientAddress;
int status, bytesRcv;
socklen_t addrSize;
// This is the start of new log
log("===============================================================================");
// Register the SIGINT handler
signal(SIGINT, signalHandler);
// Create output folder if there isn't any
createOutputFolder();
// Create a server socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0){
log("SERVER ERROR: Could not open socket");
exit(-1);
}
// Setup server address
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons((unsigned short) SERVER_PORT);
// Bind the server socket
status = bind(serverSocket, (struct sockaddr *) &serverAddress, sizeof(serverAddress));
if (status < 0){
log("SERVER ERROR: Could not bind socket");
exit(-1);
}
// Listen up to 10 clients
status = listen(serverSocket, 10);
if (status < 0){
log("SERVER ERROR: Could not listen on socket");
exit(-1);
}
std::cout << "SERVER: READY" << std::endl;
log("SERVER: READY");
// Wait for clients
while(keepRunning){
addrSize = sizeof(clientAddress);
clientSocket = accept(serverSocket, (struct sockaddr *) &clientAddress, &addrSize);
if (clientSocket < 0){
if(!keepRunning){
break;
}
log("SERVER ERROR: Could not accept incoming client connection");
continue;
}
// If clientSocket != -1
std::thread(handleClient, clientSocket).detach();
}
// Server shutdown after SIGINT
std::cout << "SERVER: Server shutdown" << std::endl;
log("SERVER: Server shutdown");
}
void signalHandler(int signum){
log("SERVER: SIGINT detected. Shutting down server");
keepRunning = false;
// Only shutdown server if serverSocket has been properly created
if(serverSocket != -1){
shutdown(serverSocket, SHUT_RDWR); // Force shutdown so that the code doesn't wait for accept() in main
close(serverSocket);
}
}
void createOutputFolder(){
std::filesystem::path dir = "./output/";
if(!std::filesystem::exists(dir)){
std::filesystem::create_directories(dir);
}
}
// Handle each client separately
int handleClient(int clientSocket){
int bytesRcv;
char buffer[30];
char response[] = "OK" ;
std::cout << "SERVER: Received client connection" << std::endl;
log("SERVER: Received client connection");
// Infinite loop to talk to client
while(1) {
// Receive messages from the client
bytesRcv = recv(clientSocket, buffer, sizeof(buffer), 0);
if (bytesRcv < 0){
log("SERVER ERROR: bytesRcv < 0 error");
break;
}
buffer[bytesRcv] = 0; // Put a 0 at the end so we can display the string
std::cout << "SERVER: Received client request: " << buffer << std::endl;
// Respond with "OK" message
std::cout << "SERVER: Sending \"" << response << "\" to client" << std::endl;
send(clientSocket, response, strlen(response), 0);
if((strcmp(buffer, "done") == 0) || (strcmp(buffer, "stop") == 0)){
break;
}
else if (strcmp(buffer, "upload") == 0){
// Call the handleUpload function
std::cout << "SERVER: --------------------------------" << std::endl;
std::cout << "SERVER: --- < Entering Upload Mode > ---" << std::endl;
log("SERVER: --- < Entering Upload Mode > ---");
handleUpload(clientSocket);
}
else if (strcmp(buffer, "download") == 0){
// Call the handleDownload function
std::cout << "SERVER: ----------------------------------" << std::endl;
std::cout << "SERVER: --- < Entering Download Mode > ---" << std::endl;
log("SERVER: --- < Entering Download Mode > ---");
handleDownload(clientSocket);
send(clientSocket, response, strlen(response), 0);
}
}
std::cout << "SERVER: Closing client connection" << std::endl;
log("SERVER: Closing client connection");
close(clientSocket);
return 0;
}
int handleUpload(int clientSocket){
// While loop to keep accepting new files being uploaded
// Without this loop, server only receives 1 file
while(1){
FileHeader recvFile;
int bytesRcv = recv(clientSocket, &recvFile, sizeof(recvFile), 0);
//std::cout << "bytesRcv = " << bytesRcv << std::endl;
if (bytesRcv < 0){
log("SERVER ERROR: bytesRcv < 0 error");
}
if (strcmp(recvFile.fn, "abort") == 0){
std::cout << "SERVER: --- < Aborting Upload Mode > ---" << std::endl;
log("SERVER: --- < Aborting Upload Mode > ---");
return 0;
}
if (strcmp(recvFile.fn, "done") == 0){
std::cout << "SERVER: --- < Finishing Upload Mode > ---" << std::endl;
log("SERVER: --- < Finishing Upload Mode > ---");
return 0;
}
std::cout << "SERVER: File's name is: " << recvFile.fn << std::endl;
std::string fnStr(recvFile.fn);
log("SERVER: File's name is: " + fnStr);
std::cout << "SERVER: File's size is: " << recvFile.fs << std::endl;
log("SERVER: File's size is: " + std::to_string(recvFile.fs));
clientUpload(clientSocket, recvFile.fn, recvFile.fs);
}
return 0;
}
// This function handles upload of 1 file
int clientUpload(int clientSocket, const char fileName[128], int fileSize){
int totalBytesRead = 0;
char buffer[maxBufferLength];
std::string fullPath = "./output/";
fullPath += fileName;
log("SERVER: Client uploading");
std::ofstream outFile(fullPath, std::ofstream::binary);
if (!outFile){
log("SERVER ERROR: Could not write outFile");
return -1;
}
while(totalBytesRead < fileSize){
int bytesToRead = std::min(maxBufferLength, fileSize - totalBytesRead);
int bytesRead = recv(clientSocket, buffer, bytesToRead, 0);
if (bytesRead < 0){
log("SERVER: Writing error (bytesRead < 0)");
break;
}
//std::cout << "total bytes read = " << totalBytesRead << std::endl;
outFile.write(buffer, bytesRead);
totalBytesRead += bytesRead;
}
// Send the client acknowledgement that the file was received
const char* ackMsg = "file_received";
int ackBytes = send(clientSocket, ackMsg, strlen(ackMsg), 0);
if (ackBytes > 0){
std::string fileNameStr(fileName);
log("SERVER: Server has received file: " + fileNameStr);
}
outFile.close();
log("SERVER: File closed");
return 0;
}
// This function will handle all of the download feature
int handleDownload(int clientSocket){
// While loop to keep accepting new files being downloaded from clients
// Without this loop, server only processes 1 file
char buffer[maxBufferLength];
int bytesSend;
FileHeader requestFile;
while(1){
// Flush the buffer
memset(buffer, 0, sizeof(buffer));
// Server receives files' names from the client (clients request these files)
int downFile = recv(clientSocket, buffer, sizeof(buffer), 0);
if (downFile < 0){
log("SERVER ERROR: downFile < 0 error");
}
std::cout << "SERVER: The requested file is: " << buffer << std::endl;
if(strcmp(buffer, "abort") == 0){
std::cout << "SERVER: --- < Aborting Download Mode > ---" << std::endl;
log("SERVER: --- < Aborting Download Mode > ---");
return 0;
}
if (strcmp(buffer, "done") == 0 || std::filesystem::exists(buffer) && std::filesystem::is_regular_file(buffer)){
// if buffer == "done". No real file's name is like this so the server will understand this is a signal to stop
if (strcmp(buffer, "done") == 0){
break;
}
// Otherwise, proceed as usual
// Open the file (or try to, at least)
std::ifstream inFile(buffer, std::ios::binary);
// If cannot open inFile
if (!inFile){
log("SERVER ERROR: Could not open inFile");
return -1;
}
// Get the size of the file
inFile.seekg(0, inFile.end);
int fileSize = inFile.tellg();
inFile.seekg(0, inFile.beg);
// Send file's meta data to client
strcpy(requestFile.fn, buffer);
requestFile.fs = fileSize;
bytesSend = send(clientSocket, &requestFile, sizeof(requestFile), 0);
if(bytesSend < 0){
log("SERVER ERROR: bytesSend < 0 error");
}
std::cout << "SERVER: Sent metadata of file " << requestFile.fn << " to client" << std::endl;
std::string requestFileNameStr(requestFile.fn);
log("SERVER: Sent metadata of file " + requestFileNameStr + " to client");
if(bytesSend < 0){
log("SERVER ERROR: bytesSend < 0 error");
}
// Read inFile into buffer
while (inFile.read(buffer, maxBufferLength) || inFile.gcount() > 0){
std::streamsize bytesRead = inFile.gcount();
//std::cout << bytesRead << std::endl;
// Sending the data to the client
bytesSend = send(clientSocket, buffer, bytesRead, 0);
if(bytesSend < 0){
log("SERVER ERROR: bytesSend < 0 error");
}
}
std::cout << "File: " << requestFile.fn << " - Done" << std::endl;
log("File: " + requestFileNameStr + " - Done");
// Wait for the acknowledgement from the client
char ackBuffer[80];
int ackBytes = recv(clientSocket, ackBuffer, sizeof(ackBuffer), 0);
if (ackBytes > 0){
ackBuffer[ackBytes] = '\0';
std::cout << "SERVER: Client received file " << requestFile.fn << std::endl;
log("SERVER: Client received file " + requestFileNameStr);
}
}
else{
std::cout << "SERVER: File requested does not exist or inaccessible" << std::endl;
log("SERVER: File requested does not exist or inaccessible");
// Send "done" - which is a fake file to let the client know that the file does not exist
strcpy(requestFile.fn, "done");
requestFile.fs = 0;
bytesSend = send(clientSocket, &requestFile, sizeof(requestFile), 0);
if (bytesSend < 0){
log("SERVER ERROR: bytesSend < 0 error");
}
continue;
}
}
std::cout << "SERVER: --- < Finishing Download Mode > ---" << std::endl;
log("SERVER: --- < Finishing Download Mode > ---");
return 0;
}
// This function handles logging
void log(const std::string& message){
std::lock_guard<std::mutex> guard(logMutex);
std::time_t now = std::time(nullptr);
logFile << std::ctime(&now) << ">> " << message << std::endl;
}