Skip to content

Commit 985861b

Browse files
committed
Add some verifications for limit tests cases
1 parent f8b82a1 commit 985861b

File tree

6 files changed

+91
-25
lines changed

6 files changed

+91
-25
lines changed

Client/client

152 Bytes
Binary file not shown.

Client/client.c

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,51 @@ int main(){
2525
for (int i = 0; i < 2 && tokenized != NULL; ++i) {
2626
tokenized = strtok_r(NULL, " ", &saveptr);
2727
}
28-
if(tokenized != NULL){
28+
if (tokenized != NULL) {
2929
char msg[1024];
3030
//open the file "tokenized"
3131
FILE *file = fopen(tokenized, "r");
32-
//write all content in msg
33-
fseek(file, 0, SEEK_END);
34-
long file_size = ftell(file);
35-
fseek(file, 0, SEEK_SET);
36-
fread(msg, 1, file_size, file);
37-
msg[file_size] = '\0';
38-
fclose(file);
39-
//send msg
40-
printf("Envoi du fichier...\n");
41-
sndmsg(msg, 8080);
42-
printf("Fichier envoyé\n");
43-
char msg2[1024];
44-
getmsg(msg2);
45-
printf("%s\n", msg2);
32+
if (file == NULL) {
33+
printf("Erreur lors de l'ouverture du fichier, votre fichier n'existe pas dans le bon chemin\n");
34+
char msg2[1024] = "ERROR";
35+
sndmsg(msg2, 8080);
36+
}else{
37+
//write all content in msg
38+
fseek(file, 0, SEEK_END);
39+
long file_size = ftell(file);
40+
fseek(file, 0, SEEK_SET);
41+
fread(msg, 1, file_size, file);
42+
msg[file_size] = '\0';
43+
fclose(file);
44+
//send msg
45+
printf("Envoi du fichier...\n");
46+
sndmsg(msg, 8080);
47+
printf("Fichier envoyé\n");
48+
char msg2[1024];
49+
getmsg(msg2);
50+
printf("%s\n", msg2);
51+
}
52+
}
53+
}else if (strncmp(input,"sectrans -down", 14) == 0){
54+
printf("Reception dun fichier\n");
55+
char *copy = strdup(input);
56+
char *saveptr;
57+
char *tokenized = strtok_r(copy, " ", &saveptr);
58+
for (int i = 0; i < 2 && tokenized != NULL; ++i) {
59+
tokenized = strtok_r(NULL, " ", &saveptr);
60+
}
61+
if (tokenized != NULL) {
62+
char msg[1024];
63+
getmsg(msg);
64+
if(strncmp(msg,"Erreur",6) == 0){
65+
printf("%s", msg);
66+
}else{
67+
FILE *file = fopen(tokenized, "w");
68+
//write all msg in the new file
69+
fwrite(msg, 1, strlen(msg), file);
70+
fclose(file);
71+
printf("Fichier bien reçu\n");
72+
}
4673
}
4774
} else {
4875
printf("Commande inconnue\n");

Client/empty_file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
happy christmas to you and your family

Server/database/empty_file.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
happy christmas to you and your family

Server/server

152 Bytes
Binary file not shown.

Server/server.c

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,57 @@ int main(){
5252
}
5353
if (tokenized != NULL) {
5454
char msg[1024];
55-
//create the file on the database folder
55+
getmsg(msg);
56+
if(strncmp(msg,"ERROR",5) == 0){
57+
printf("Erreur lors de la reception du fichier\n");
58+
}else{
59+
//create the file on the database folder
60+
const char *directory = "./database/";
61+
char filepath[1024];
62+
strcpy(filepath, directory);
63+
strcat(filepath, tokenized);
64+
FILE *file = fopen(filepath, "w");
65+
//write all msg in the new file
66+
fwrite(msg, 1, strlen(msg), file);
67+
fclose(file);
68+
printf("Fichier bien reçu\n");
69+
70+
char msg2[1024] = "Fichier bien reçu";
71+
sndmsg(msg2, 8081);
72+
}
73+
}
74+
}else if (strncmp(rcv_msg,"sectrans -down", 14) == 0){
75+
char *copy = strdup(rcv_msg);
76+
char *saveptr;
77+
char *tokenized = strtok_r(copy, " ", &saveptr);
78+
for (int i = 0; i < 2 && tokenized != NULL; ++i) {
79+
tokenized = strtok_r(NULL, " ", &saveptr);
80+
}
81+
if (tokenized != NULL) {
82+
char msg[1024];
83+
//open the file "tokenized"
5684
const char *directory = "./database/";
5785
char filepath[1024];
5886
strcpy(filepath, directory);
5987
strcat(filepath, tokenized);
60-
FILE *file = fopen(filepath, "w");
61-
getmsg(msg);
62-
//write all msg in the new file
63-
fwrite(msg, 1, strlen(msg), file);
64-
fclose(file);
65-
printf("Fichier bien reçu\n");
66-
67-
char msg2[1024] = "Fichier bien reçu";
68-
sndmsg(msg2,8081);
88+
FILE *file = fopen(filepath, "r");
89+
if (file == NULL) {
90+
printf("Erreur lors de l'ouverture du fichier pour l'envoi\n");
91+
char msg2[1024] = "Erreur : Le fichier demandé n'existe pas\n";
92+
sndmsg(msg2, 8081);
93+
}else{
94+
//write all content in msg
95+
fseek(file, 0, SEEK_END);
96+
long file_size = ftell(file);
97+
fseek(file, 0, SEEK_SET);
98+
fread(msg, 1, file_size, file);
99+
msg[file_size] = '\0';
100+
fclose(file);
101+
//send msg
102+
printf("Envoi du fichier...\n");
103+
sndmsg(msg, 8081);
104+
printf("Fichier envoyé\n");
105+
}
69106
}
70107
}else{
71108
printf("Commande inconnue\n");

0 commit comments

Comments
 (0)