bonjour,

j’essaie actuellement de développer un mini-serveur de fichier cependant lors de la réception des fichiers le contenue de ceux ci est quasiment correct :
l'intégralité du fichier est présente mais 300 octet supplémentaire son aussi présent à la fin.
comme il s'agit entièrement de fait maison (le protocole utilisé aussi) je vous mais à disposition l'intégralité du code, si jamais vous avez des idées pour optimisé le code o le sécurisé où même n'importe quelle suggestion je suis prenant
sachant que le client et le serveur utilise SocketC+S.c et .h.
de plus il faut compiler les main.c des deux sous linux je n'ai pas tester sous windows si cela se compile ou pas (en theorie oui)
si vous avez besoin de quelqu'info qu'il sois demander moi
je vous donne d'abord le code du serveur
SERVER

SocketC+S.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "SocketC+S.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
 
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
static  void init(){
    #ifdef WIN32
    WSADATA wsa;
    int err =WSAStartup⁽MAKEWORD(2, 2),&wsa);
    if(err<0)
    {
        puts("WSAstartup failed !\n");
        exit(EXIT_FAILURE);
    }
    #endif // WIN32
}
/** \brief initialisation de l'interface winsock
 * \return void
 */
static void end(SOCKET sock){
closesocket(sock);
#ifdef WIN32
    WSACleanup();
#endif // WIN32
 
}
/** \brief fin de l'interface winsock
 *  fermeture du socket
 * \param socket a fermer (socket principal)
 * \return void
 */
 static void Dsend(char *buffer, SOCKET sock){
    int size=strlen(buffer);
    if(send(sock,&size,sizeof(int),0)<0)
    {
        perror("send()");
        exit(errno);
    }
    if(send(sock,buffer,strlen(buffer),0)<0)
    {
        perror("send()");
        exit(errno);
    }
}
/** \brief envois de la taille du buffer
 *  envois du buffer sur le socket
 * \param buffer a envoyer
 * \param socket sur lequel envoyer
 * \return void
 */
 
static  int Drecv(char *buffer,SOCKET csock){
       int size=0;
        if(recv(csock, &size,sizeof(int),0)<0)
   {
       perror("recv()");
       exit(errno);
   }
 
 if(recv(csock, buffer,size*sizeof(char),0)<0)
   {
       perror("recv()");
       exit(errno);
   }
    buffer[size]='\0';
return size;
}
/** \brief reception des infos envoyer par Dsend
 *
 * \param buffer a remplir
 * \param socket client sur lequel ecouter
 * \return taille du buffer
 */
 
 static int Csignal(int buffer,SOCKET sock){
int res=0;
if(send(sock,&buffer,sizeof(buffer),0)<0){
        perror("send()");
        exit(errno);
    }
printf("signal %u sent\n",buffer);
if(recv(sock, &res,sizeof(int),0)<0){
        perror("recv()");
        exit(errno);
    }
if(res!=1){
        printf("reponse incorecte\n");
        return FALSE;
    }else{return TRUE;
printf("signal %u recu\n",res);
}
}
/** \brief envoie du signal de choix
 * \param signal a envoyer
 * \param socket sur lequel envoyer
 * \return 0 si reponse incorecte du serveur
 *  1 si reponse correcte du serveur
 */
 
static int Rsignal(SOCKET sock){
int buffer=0;
int res=0;
if(recv(sock, &res,sizeof(int),0)<0){
        perror("recv()");
        exit(errno);
    }
    switch(res){
    case 1:buffer=1;break;
    case 666:buffer=1;break;
    default:buffer=0;break;
    }
    printf("signal reçu: %u\n",buffer);
if(send(sock,&buffer,sizeof(buffer),0)<0){
        perror("send()");
        exit(errno);
    }
printf("Reponse au signal: %u\n",buffer);
 
return res;
}
/** \brief reception du signal de Csignal
 * \param socket sur lequel recevoir
 * \return valeur du signal
 */
static FILE *RFopen(char *nomFichier,char *dossier){
 
char *location=strdup(dossier);
	if( location == NULL ){
		perror("Erreur allocation mémoire\n");
		exit( EXIT_FAILURE );
	}
char *ptr_loc=NULL;
FILE *fichier;
 
ptr_loc=realloc(location,sizeof(char)*(strlen(location)+strlen(nomFichier)-1));
if( ptr_loc == NULL ){
		free( location );
		location = NULL;
		perror("Erreur reallocation\n");
		exit( EXIT_FAILURE );
	}
location=ptr_loc;
 
strcat(location,nomFichier);
    fichier=fopen(location,"wb");
    if (fichier==NULL) {
    perror("fopen()");
    exit(errno);
    }else{
    printf("Fichier ouvert: %s \nDans %s\n",nomFichier,dossier);}
    return fichier;
}
/** \brief ouverture d'un fichier en mode binaire
 * \param nom du fichier
 * \param dossier de stockage du fichier
 * \return file descriptor
 */
 
static char *chNom(char *input){
    char* carac=input;
    int i;
    char* nom;
    for (i=0; i<128; i++)
    {
        if (!*carac) break;
        nom=carac;
        while(*carac && *carac!=DELIMITATEUR) carac++;
        if (*carac)
        {
            carac++;
        }
    }
    return nom;
}
/** \brief recuperation d'un nom de fichier dans un chemin
 * non destructif
 * \param chemin du fichier
 * \param none
 * \return nom du fichier
 */
 
  static void Lerase(){
    printf("\x0d");
    printf("\033[K");
}
/** \brief efface la ligne actuel de stdout
 * \param none
 * \param none
 * \return void
 */
 
  static char *readFile(char *path){
  FILE * fichier;
  long lSize;
  char * buffer;
  size_t result;
 
  fichier = fopen ( path , "rb" );
  if (fichier==NULL) {
    perror("fopen()");
    exit(errno);
    }
  // obtain file size:
  fseek (fichier , 0 , SEEK_END);
  lSize = ftell (fichier);
  rewind (fichier);
  // allocate memory to contain the whole file:
  buffer = (char*)malloc(sizeof(char)*lSize);
  if (buffer == NULL)  {
    perror("malloc()");
    exit(errno);
    }
  // copy the file into the buffer:
  result = fread (buffer,1,lSize,fichier);
  if (result != lSize)  {
    perror("fread()");
    exit(errno);
    }
  /* the whole file is now loaded in the memory buffer. */
  // terminate
  fclose (fichier);
  return buffer;
  free(buffer);
}
/** \brief enregistre un fichier binaire en ram
 * \param chemin du fichier
 * \param none
 * \return pointeur sur le fichier en ram
 */
 
static long sizeOfFile(char *path){
  long lSize;
  FILE *fichier = fopen ( path , "rb" );
  if (fichier==NULL) {
    perror("fopen()");
    exit(errno);
    }
  fseek (fichier , 0 , SEEK_END);
  lSize = ftell (fichier);
  rewind (fichier);
  return lSize;
}
/** \brief recupere le nombre de byte du fichier
 * \param chemin du fichier
 * \param none
 * \return nombre de byte
 */
 
 static char *decoupeStream(char *buffer){
char *output=(char*)malloc(sizeof(char)*BUFFERSIZE);
int i;
for(i=0;i<BUFFERSIZE;i++)
{
    output[i]=*buffer;
    buffer++;
}
output[i]='\0';
return output;
}
/** \brief decoupe un buffer en segment de BUFFERSIZE byte
 * destructif
 * \param buffer a decouper
 * \param
 * \return partie du buffer faisant BUFFERSIZE
 */
 
static void sFile(char *path,SOCKET sock){
    char* nom=chNom(path);
    printf("le nom est '%s',le chemin est '%s'\n",nom,path);
    Csignal(1,sock);
    Dsend(nom,sock);
    envoieFile(path,sock);
}
/** \brief envoie d'un fichier avec prérequis:
 * Csignal; Dsend(nom); envoieFile
 * \param chemin du fichier
 * \param socket sur lequel envoyer
 * \return void
 */
 
static void envoieFile(char *path ,SOCKET sock){
    long i=0,size=0;
    char *ligne=malloc(sizeof(char)*BUFFERSIZE);
    char *file=readFile(path);
    printf("sending file : %s\n",path);
    size=sizeOfFile(path);
    for(i=0;i<size;i=i+BUFFERSIZE)
    {
        ligne=decoupeStream(file);
        printf(".");
        Dsend(ligne,sock);
 
    }
    Dsend("EOF",sock);
    free(ligne);
    free(file);
}
/** \brief envoie du corp d'un fichier
 * \param chemin du fichier
 * \param socket sur lequel envoyer
 * \return void
 */
 
static void frecv(SOCKET csock,char *dossier){
    int ctrl=1;
    char buffer[BUFFERSIZE];
    char nomFichier[BUFFERSIZE];
    Drecv(nomFichier,csock);
    FILE *fichier=RFopen(nomFichier,dossier);
    while(ctrl>0)
   {
    ctrl=Drecv(buffer,csock);
    if(ctrl==3){
    if(!strcmp(buffer,"EOF")){
            printf("\nEnd of file!!!\n");
            break;
    }else{
    fwrite( buffer , sizeof(buffer[0]) , sizeof(buffer)/sizeof(buffer[0]) , fichier);
    }
    }
    else{
    fwrite( buffer , sizeof(buffer[0]) , sizeof(buffer)/sizeof(buffer[0]) , fichier);
        }
   }
    fclose(fichier);
}
/** \brief reception d'un fichier
 * nom + corp
 * \param socket sur lequel ecouter
 * \param dossier ou stocker le fichier
 * \return void
 */
 
static SOCKET Caccept(SOCKET sock){
    SOCKADDR_IN csin= {0};
    SOCKET csock;
    socklen_t sinsize=sizeof(csin);
    csock=accept(sock,(SOCKADDR *)&csin,&sinsize);
    if(csock==INVALID_SOCKET){
        perror("accept()");
        exit(errno);
    }
 return(csock);
}
/** \brief creation d'un socket client
 * \param socket initial
 * \param none
 * \return socket client
 */
SocketC+S.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef SOCKETCS_INCLUDED
#define SOCKETCS_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
 
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
static int Rsignal(SOCKET sock);
static int Csignal(int buffer,SOCKET sock);
static int Drecv(char *buffer,SOCKET csock);
static void Dsend(char *buffer, SOCKET soc);
static void end(SOCKET sock);
static void init();
static FILE *RFopen(char *nomFichier,char *dossier);
static char *chNom(char *input);
static void Lerase();
static char *readFile(char *path);
static long sizeOfFile(char *path);
static char *decoupeStream(char *buffer);
static void envoieFile(char *path ,SOCKET sock);
static void sFile(char *path,SOCKET sock);
static void frecv(SOCKET csock,char *dossier);
static SOCKET Caccept(SOCKET sock);
 
 
#endif // SOCKETC+S
server.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "SocketC+S.c"
#include "SocketC+S.h"
#define MAXCLIENT 100
#define MAXLOOPS 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
 
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
static SOCKET Caccept(SOCKET sock);
static void Sinit( SOCKET sock);
static void frecv(SOCKET csock,char *location);
static void gClient(SOCKET sock);
static int server();
 
 
static void Sinit( SOCKET sock){
SOCKADDR_IN sin= {0};
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port=htons(PORT);
sin.sin_family = AF_INET;
if(sock==INVALID_SOCKET){
    perror("socket()");
    exit(errno);
}
if(bind(sock,(SOCKADDR *)&sin,sizeof(sin))==SOCKET_ERROR){
    perror("bind()");
    exit(errno);
    }
}
 
 
static void gClient(SOCKET sock){
    int ex=0,choix=0;
    SOCKET csock=Caccept(sock);
    printf("Client accépter!\n");
    while(ex==0)
    {
        choix=Rsignal(csock);
        switch(choix){
        case 1:frecv(csock,"/home/tagashy/test2/");
        break;
        case 666:ex=1;
        break;
        default:printf("choix incorect !!! choix=%u\n",choix);
        break;
        }
    }
    closesocket(csock);
}
 
static int server(){
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
init();
Sinit(sock);
 
if(listen(sock,MAXCLIENT)==SOCKET_ERROR){
    perror("listen()");
    exit(errno);
}
printf("Waiting for connection ...\n");
 
   gClient(sock);
    end(sock);
    return 0;
}
Main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>
#include "server.c"
int main()
{
server();
    return 0;
}

CLIENT

main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include "client.c"
#include "SocketC+S.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
 
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
 
int main()
{
    SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
    init();
    Cinit(sock,"192.168.1.27");
    sFile("/home/tagashy/test.txt",sock);
    sFile("/home/tagashy/ikigezu.dg",sock);
    Csignal(666,sock);
    end(sock);
    return 0;
}
client.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include "SocketC+S.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
 
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
#include "SocketC+S.c"
static void Cinit(SOCKET sock,char *addres){
    SOCKADDR_IN sin= {0};
    if(sock==INVALID_SOCKET){
    perror("socket()");
        exit(errno);
    }
    sin.sin_addr.s_addr=inet_addr(addres);
    sin.sin_port=htons(PORT);
    sin.sin_family = AF_INET;
    if(connect(sock,(SOCKADDR *) &sin,sizeof(SOCKADDR))== SOCKET_ERROR){
        perror("connect()");
        exit(errno);
    }
}
merci