Salut,

J'essaie de modifier mon code serveur/client pour pouvoir envoyer des fichiers entiers. Du coté, serveur, je dispose de la fonction qui me lit le fichier que je lui désigne puis me l'envoie avec la fonction send () :

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
 
char nom[50] = "c:\\fichier_send.txt";
int fin, current;
FILE *fichier_src = fopen (nom, "rb");
if (fichier_src == NULL)
{
   perror ("fopen() ");
   exit (0); //exit(EXIT_FAILURE)
}
 
fseek(fichier_src, 0, SEEK_END);
fin=ftell(fichier_src);
/* On se place a l'endroit qui va bien dans le binaire */
fseek (fichier_src, 0, SEEK_SET);
current = 0; /* Détermination de la position courante dans la fichier */
 
#define BUFSIZE 1024
int nb_lu;
char buffer[BUFSIZE];
memset (buffer, 0, BUFSIZE);
nb_lu = fread (buffer, sizeof (char), BUFSIZE, fichier_src);
int nb_send;
int inc = 1;
while ((nb_lu > 0) && (nb_lu <= fin - current))
{
   if ((nb_send = send (csock, buffer, nb_lu, 0)) == -1)
      {
         perror ("send() ");
         exit (-1);
      }
 
   printf ("nb_send(boucle) \'%d \' = %d\n", inc, nb_send);
   if (nb_send == -1)
      perror ("send () ");
   current = current + nb_lu;
   memset (buffer, 0, BUFSIZE);
   inc++;
   if (fin - current < BUFSIZE)
   {
      break;
   }
   else
   nb_lu = fread (buffer, sizeof (char), BUFSIZE, fichier_src);
}
 
memset (buffer, 0, BUFSIZE);
fread (buffer, sizeof (char), fin - current, fichier_src);
if ((nb_send = send (csock, buffer, fin - current, 0)) == -1);
{
   perror ("write() ");
}
printf ("nb_send = %d\n", nb_send);
fclose (fichier_src);
Mais mon problème se situ au niveau du client qui reçoit les données transmises par send(). Quand je lance le transfère d'un paragraphe de texte, la fonction recv() me le copie dans un fichier et répète la copie infiniment (en tout cas, ça n'a pas l'air de s'arrêter). Puis j'ai remarqué je mets un temps infini pour sortir de la boucle while. La taille du fichier en sortie est très lourd (40MB) pour un petit paragraphe. Est ce que vous pouvez m'indiquer comment améliorer ma fonction recv() ?
merci

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
 
char nom[50] = "fichierRecubis.txt";
        FILE *fichier_src = fopen (nom, "wb");
        if (fichier_src == NULL)
            {
                    perror ("fopen() ");
                        exit (0); //exit(EXIT_FAILURE);
                }
 
    int bytesrecu=0;
    int totalbytes=0;
 
    /* Si l'on reçoit des informations : on les affiche à l'écran */
        if(bytesrecu=recv(sock, buffer, 1024, 0) != SOCKET_ERROR)
    {
    totalbytes += bytesrecu;
    fwrite(buffer,1024, 1,fichier_src);
        /*printf("Recu : %s\n", buffer);*/
    }
 
    while(totalbytes<4096)
    {
 
        /* Si l'on reçoit des informations : on les affiche à l'écran */
        if(bytesrecu=recv(sock, buffer, 1024, 0) != SOCKET_ERROR)
    totalbytes += bytesrecu;
    fwrite(buffer,1024, 1,fichier_src);
        /*printf("Recu : %s\n", buffer);*/
 
    }
 
    fclose(fichier_src);
        }
        /* sinon, on affiche "Impossible de se connecter" */
        else
        {
            printf("Impossible de se connecter\n");
        }
PS : ne soyez pas choqué par mon code, je suis un étudiant en physique qui s'intéresse à l'informatique