-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
283 lines (244 loc) · 8.56 KB
/
client.c
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
#include "helper.h"
#define PORT 8000
void handle_Upload(int clientSocket, char *upload_download_command)
{
// int option = 1;
// send(clientSocket, &option, sizeof(option), 0);
send(clientSocket, &(int){1}, sizeof(int), 0);
const char *fileName = upload_download_command + 8;
send(clientSocket, fileName, strlen(fileName), 0);
// Delimiter of newline is send, to separate file name size
send(clientSocket, "\n", 1, 0);
int fileDescriptor = open(fileName, O_RDONLY);
if (fileDescriptor < 0)
{
perror("File Not found | Error opening file :/");
close(clientSocket);
return;
}
char *sizeStr = getFileSize(fileName);
printf("File size: %s bytes\n", sizeStr);
// Send the file size as a string
send(clientSocket, sizeStr, strlen(sizeStr), 0);
printf("File Sent to the Server.\n");
char response[MAX_SIZE];
int bytesRead = recv(clientSocket, response, sizeof(response) - 1, 0);
if (bytesRead > 0)
{
response[bytesRead] = '\0';
printf("\nServer response: %s\n", response);
if (strcmp(response, "Out of space.") == 0)
{
printf("Server indicates out of space.\n");
}
else if (strcmp(response, "Error parsing config file.") == 0 || strcmp(response, "Error updating config file.") == 0 || strcmp(response, "Error writing to config file.") == 0)
{
printf("Server error: %s\n", response);
}
else if (strcmp(response, "File already exists.") == 0)
{
handleFileExistsResponse(clientSocket, fileName);
}
else
{
printf("Server message: %s\n", response);
uploadFile(clientSocket, fileName);
}
}
else
{
printf("Failed to receive server response.\n");
}
close(fileDescriptor);
}
void handle_Downlaod(int clientSocket, char *upload_download_command)
{
// option = 2;
// send(clientSocket, &option, sizeof(option), 0);
send(clientSocket, &(int){2}, sizeof(int), 0);
const char *fileName = upload_download_command + 10;
send(clientSocket, fileName, strlen(fileName), 0);
char response[MAX_SIZE];
int bytesReceived = recv(clientSocket, response, sizeof(response) - 1, 0);
if (bytesReceived > 0)
{
response[bytesReceived] = '\0';
printf("Server response: %s\n", response);
if (strcmp(response, "File found.") == 0)
{
printf("File '%s' is available for download.\n", fileName);
downloadFile(clientSocket, fileName);
}
else if (strcmp(response, "File not found.") == 0)
{
printf("The requested file '%s' does not exist on the server.\n", fileName);
}
else
{
printf("Unexpected server response: %s\n", response);
}
}
else
{
printf("Failed to receive response from the server.\n");
}
}
void handle_ViewFiles(int clientSocket, char *upload_download_command)
{
send(clientSocket, &(int){3}, sizeof(int), 0);
view_files(clientSocket);
}
void handle_Delete_File(int clientSocket, char *upload_download_command)
{
// option = 4;
// send(clientSocket, &option, sizeof(option), 0);
send(clientSocket, &(int){4}, sizeof(int), 0);
const char *fileName = upload_download_command + 8;
deleteFileRequest(clientSocket, fileName);
}
void handle_Update_File(int clientSocket, char *upload_download_command)
{
send(clientSocket, &(int){5}, sizeof(int), 0);
const char *fileName = upload_download_command + 8;
update_file(clientSocket, fileName);
}
int main()
{
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == -1)
{
perror("Error creating socket");
return 1;
}
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(PORT);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) == -1)
{
perror("Error connecting to server");
return 1;
}
// Receive server's initial response
char buffer[50];
int bytesReceived = recv(clientSocket, buffer, 49, 0);
if (bytesReceived < 0)
{
perror("Error receiving message from server");
close(clientSocket);
return 1;
}
buffer[bytesReceived] = '\0';
if (strstr(buffer, "Server is busy. Please try again later\0") != NULL)
{
printf("Server response: %s\n", buffer);
close(clientSocket);
return 0;
}
printf("Server response: %s\n", buffer);
printf("Type:\n$REGISTER$ for Signing Up\n$LOGIN$ for Signing In\n");
char command[512];
int auth_code;
int count = 0;
do
{
if(count > 0)
printf("Invalid command.\n");
printf("-> ");
scanf("%s", command);
count++;
} while (strncmp(command, "$REGISTER$", 10) != 0 && strncmp(command, "$LOGIN$", 7) != 0);
if (strncmp(command, "$REGISTER$", 10) == 0)
{
auth_code = 1;
send(clientSocket, &auth_code, sizeof(auth_code), 0);
char userName[MAX_SIZE];
printf("\nEnter username: ");
scanf("%s", userName);
send(clientSocket, userName, strlen(userName), 0);
char password[MAX_SIZE];
printf("Enter password: ");
scanf("%s", password);
send(clientSocket, password, strlen(password), 0);
int userExists;
recv(clientSocket, &userExists, sizeof(userExists), 0);
if (userExists == 1)
{
printf("$ \"%s\"'s ACCOUNT HAS BEEN REGISTERED$\n", userName);
}
else if (userExists == 0)
{
printf("$ ACCOUNT FOR \"%s\" ALREADY EXISTS$\n", userName);
}
else
{
printf("$ ERROR: Invalid response from server $\n");
}
}
else if (strncmp(command, "$LOGIN$", 7) == 0)
{
auth_code = 2;
send(clientSocket, &auth_code, sizeof(auth_code), 0);
// Authenticate user
char userName[MAX_SIZE];
printf("Enter username: ");
scanf("%s", userName);
send(clientSocket, userName, strlen(userName), 0);
char password[MAX_SIZE];
printf("Enter password: ");
scanf("%s", password);
send(clientSocket, password, strlen(password), 0);
char response[MAX_SIZE];
recv(clientSocket, response, sizeof(response) - 1, 0);
response[sizeof(response) - 1] = '\0';
printf("Server: %s\n", response);
if (strcmp(response, "User found") == 0)
{
printf("\nAvailable Commands:\n\n$UPLOAD$<file-name> for Uploading Data/File\n$DOWNLOAD$<file-name> for Downloading an Uploaded Data/File\n$VIEW$ for View Uploaded Files and there sizes\n$UPDATE$<file-name> for Updating and Existing/Uploaded File\n$DELETE$<file-name> for Deleting a Uploading File\n\n");
char upload_download_command[512];
int check = 1;
do
{
printf("-> ");
scanf("%s", upload_download_command);
if (strncmp(upload_download_command, "$UPLOAD$", 8) == 0)
{
handle_Upload(clientSocket, upload_download_command);
check = 0;
}
else if (strncmp(upload_download_command, "$DOWNLOAD$", 10) == 0)
{
handle_Downlaod(clientSocket, upload_download_command);
check = 0;
}
else if (strncmp(upload_download_command, "$VIEW$", 6) == 0)
{
handle_ViewFiles(clientSocket, upload_download_command);
check = 0;
}
else if (strncmp(upload_download_command, "$DELETE$", 8) == 0)
{
handle_Delete_File(clientSocket, upload_download_command);
check = 0;
}
else if (strncmp(upload_download_command, "$UPDATE$", 8) == 0)
{
handle_Update_File(clientSocket, upload_download_command);
check = 0;
}
else if (strncmp(upload_download_command, "exit", 4) == 0)
{
printf("Exiting program.\n");
check = 0;
break;
}
else
{
printf("Invalid command. Please try again.\n");
}
} while (check == 1);
}
}
close(clientSocket);
return 0;
}