codice:
#define SIZE 8192 //8k
bool recvFile ( int file, int sock, size_t size) {
FILE * log ;
char buffer[SIZE_BLOCK];
ssize_t nread =0, nwrite=0;
ssize_t tot=size;
int i = 0;
memset(buffer, 0, SIZE_BLOCK);
printf("Inizio ricezione del file....");
printf("Devo ricevere %d\n", tot);
fflush(stdout);
log = fopen("log", "w");
if ( log == NULL)
perror("Errore log ");
fprintf(log , "Test : \n" );
while(1) {
fprintf(log,"%d\n", tot);
memset(buffer, 0, SIZE_BLOCK);
if (tot <= 0)
break;
nread=recv(sock, buffer, SIZE_BLOCK,0 );
fprintf(log,"letto %d\n" , nread);
if ( nread < 0)
perror("Errore nella lettura dal socket ");
nwrite=write(file, buffer, nread);
if ( nwrite < 0 )
perror("Errore nella scrittura del file ");
fprintf(log,"scritto %d\n", nwrite);
tot-=nwrite;
fprintf( log,"%d \t", tot);
i++;
if (tot <= 0)
break;
}
printf("Ricezione del file terminata\n");
printf("ricevuti %d ", tot);
fflush(stdout);
return 0;
}
bool sendFile ( int file, int sock, size_t size) {
FILE * log = fopen("log", "w");
char buffer[SIZE_BLOCK];
ssize_t nread,nwrite;
// numero di iterazioni che deve essere fatto per inviare l'intero file
ssize_t tot=size;
memset(buffer, 0, SIZE_BLOCK);
printf("Inizio invio del file......");
while ( TRUE) {
fprintf(log, " %d \n", tot);
memset(buffer, 0, SIZE_BLOCK);
nread=read(file, buffer, SIZE_BLOCK);
// nel caso in cui il file sia un multiplo di 1024
if ( nread < 0 )
perror("Errore lettura dal file");
fprintf(log, "Ho letto %u\n", nread);
nwrite=send(sock, buffer, nread, 0);
if ( nwrite < 0)
perror("Errore scrittura sul socket");
fprintf(log,"Ho scritto %u\n", nwrite);
tot-=nwrite;
fprintf(log, "tot %d\t", tot);
if (tot <= 0)
break;
}
printf("inviati %d\n", tot);
printf("File inviato\n");
fflush(stdout);
return 0;
}
qualcuno mi puo aiutare??'