emitrax
04-08-2004, 14:05
Salve gente... Non riesco a far andare queste funzioni che dovrebbero permettere l'invio e il ricevimento del file da parte di due client di chat.Qui il codice...
int recv_file(char *filename)
{
struct sockaddr_in my_struct, his_client;
FILE *fp;
int my_sock, nb, his_struct_size, n_try = 0;
char *filecrypt, buffer[SIZE_BUF];
struct timeval timeout; //struttura tempo per il timeout
fd_set read_fd;
/* Inizializza a zero l' fdset */
FD_ZERO( &read_fd );
/* Setto il timeout a 5 minuti */
timeout.tv_sec = 300; // secondi
timeout.tv_usec = 0; // microsendi
/* Creo il socket */
if( ( my_sock = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
{
message( "Bad socket" );
return 0;
}
/* Riempio la struttura sockaddr_in */
memset( &my_struct,0,sizeof(my_struct) );
my_struct.sin_family = AF_INET;
my_struct.sin_port = htons(DCC_PORT);
getpeername( sockt, (SA *) &his_client, &his_struct_size );
my_struct.sin_addr.s_addr = his_client.sin_addr.s_addr;
/* Crea il nome del file da ricevere */
filecrypt = (char *)malloc(strlen(filename) + EXST);
strcpy( filecrypt, filename );
strncat( filecrypt, ".cry" , 4);
if( (fp = fopen( filecrypt , "wb" )) == NULL )
{
message( "Error returned by fopen" );
return 0;
}
/* Mi connetto all'altro client per ricevere il file */
while(1)
{
/* Tenta di connettersi per 3 volte per un totale di 180 secondi*/
if( connect( my_sock, (SA *) &my_struct, sizeof(my_struct) ))
{
if( (errno = ECONNREFUSED) && (n_try <= 3) )
{
/* Aspetta 60 secondi prima di riprovare */
sleep(60);
n_try++;
continue;
}
else
{
message( "Error returned by connect " );
return 0;
}
}
else
break;
}
/* CONNESSO */
/* Ricevo il file */
for( ;; )
{
/* setta il bit di lettura */
FD_SET( my_sock, &read_fd );
/* La select azzera il bit se il socket non e' pronto in lettura */
if( (n_try = select( my_sock + 1, &read_fd, NULL, NULL, &timeout )) == -1 )
{
message( "Error returned by select");
return 0;
}
if( n_try )
{
if( FD_ISSET( my_sock, &read_fd ) )
{
nb = read( my_sock, buffer, sizeof(buffer));
fwrite( buffer, nb, 1, fp );
}
}
else
break; //Sono passati 5 minuti senza ricevere dati
}
/* Chiudo il file */
fclose(fp);
/* Decripta il file ricevuto */
// file_decrypt( filecrypt );
return 1;
}
/* invia il file collegandosi alla porta DCC_SEND */
int send_file(char *pathfile)
{
struct sockaddr_in my_struct;
char *file_to_send = NULL;
char buffer[SIZE_BUF];
FILE *fp;
int sock_fd, listen_fd, n = 0;
size_t len_buf;
/*Toglie lo /n finale */
pathfile[ strlen(pathfile) - 1 ] = '\' ;
/* Salta gli spazi */
while( isspace(pathfile[n]) )
n++;
/* Cripta il file */
// if( !file_crypt( &pathfile[n] ) )
// return 0;
/* Creo il socket */
if( ( listen_fd = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
{
message( "Bad socket" );
return 0;
}
/* Riempio la struttura sockaddr_in */
memset(&my_struct,0,sizeof(my_struct));
my_struct.sin_family = AF_INET;
my_struct.sin_port = htons( DCC_PORT );
my_struct.sin_addr.s_addr = htonl( INADDR_ANY );
/* Associo il socket alla struttura */
if( bind( listen_fd, (SA *) &my_struct, sizeof(my_struct) ) < 0 )
{
message( " Error returnd by bind " );
return 0;
}
/* Si mette in ascolto */
if( listen( listen_fd, 1 ) < 0 )
{
message( "Error returned by listen " );
return 0;
}
/* Aspetta una connessione ( in teoria potrebbe anche non rientrare ) */
if( (sock_fd = accept( listen_fd, (SA *) NULL, NULL ) ) < 0 )
{
message( "Error returned by accept " );
return 0;
}
/* CONNESSIONE RIUSCITA */
/* Chiudo il socket che era in ascolto */
close(listen_fd);
file_to_send = (char *)malloc(strlen(&pathfile[n]) + EXST);
strcpy(file_to_send, &pathfile[n]);
// strcat(file_to_send, ".cry");
/* Apre il file criptato */
if( (fp = fopen( file_to_send, "rb" ) ) == NULL )
{
message( " Error while reopening encrypted file " );
return 0;
}
/* Invia il file */
while( !feof(fp) )
{
len_buf = fread( buffer, SIZE_BUF, 1, fp );
write( sock_fd, buffer, len_buf );
}
/* Chiudo il file */
fclose(fp);
return 1;
}
In pratrica la connessione avviene..... ma il lato che invia sembra che stacchi immediatamente la connessione.Qualcuno puoi aiutarmi?! Spero che aver pastato tutto questo codice non sia un problema.
Regards
Emitrax
int recv_file(char *filename)
{
struct sockaddr_in my_struct, his_client;
FILE *fp;
int my_sock, nb, his_struct_size, n_try = 0;
char *filecrypt, buffer[SIZE_BUF];
struct timeval timeout; //struttura tempo per il timeout
fd_set read_fd;
/* Inizializza a zero l' fdset */
FD_ZERO( &read_fd );
/* Setto il timeout a 5 minuti */
timeout.tv_sec = 300; // secondi
timeout.tv_usec = 0; // microsendi
/* Creo il socket */
if( ( my_sock = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
{
message( "Bad socket" );
return 0;
}
/* Riempio la struttura sockaddr_in */
memset( &my_struct,0,sizeof(my_struct) );
my_struct.sin_family = AF_INET;
my_struct.sin_port = htons(DCC_PORT);
getpeername( sockt, (SA *) &his_client, &his_struct_size );
my_struct.sin_addr.s_addr = his_client.sin_addr.s_addr;
/* Crea il nome del file da ricevere */
filecrypt = (char *)malloc(strlen(filename) + EXST);
strcpy( filecrypt, filename );
strncat( filecrypt, ".cry" , 4);
if( (fp = fopen( filecrypt , "wb" )) == NULL )
{
message( "Error returned by fopen" );
return 0;
}
/* Mi connetto all'altro client per ricevere il file */
while(1)
{
/* Tenta di connettersi per 3 volte per un totale di 180 secondi*/
if( connect( my_sock, (SA *) &my_struct, sizeof(my_struct) ))
{
if( (errno = ECONNREFUSED) && (n_try <= 3) )
{
/* Aspetta 60 secondi prima di riprovare */
sleep(60);
n_try++;
continue;
}
else
{
message( "Error returned by connect " );
return 0;
}
}
else
break;
}
/* CONNESSO */
/* Ricevo il file */
for( ;; )
{
/* setta il bit di lettura */
FD_SET( my_sock, &read_fd );
/* La select azzera il bit se il socket non e' pronto in lettura */
if( (n_try = select( my_sock + 1, &read_fd, NULL, NULL, &timeout )) == -1 )
{
message( "Error returned by select");
return 0;
}
if( n_try )
{
if( FD_ISSET( my_sock, &read_fd ) )
{
nb = read( my_sock, buffer, sizeof(buffer));
fwrite( buffer, nb, 1, fp );
}
}
else
break; //Sono passati 5 minuti senza ricevere dati
}
/* Chiudo il file */
fclose(fp);
/* Decripta il file ricevuto */
// file_decrypt( filecrypt );
return 1;
}
/* invia il file collegandosi alla porta DCC_SEND */
int send_file(char *pathfile)
{
struct sockaddr_in my_struct;
char *file_to_send = NULL;
char buffer[SIZE_BUF];
FILE *fp;
int sock_fd, listen_fd, n = 0;
size_t len_buf;
/*Toglie lo /n finale */
pathfile[ strlen(pathfile) - 1 ] = '\' ;
/* Salta gli spazi */
while( isspace(pathfile[n]) )
n++;
/* Cripta il file */
// if( !file_crypt( &pathfile[n] ) )
// return 0;
/* Creo il socket */
if( ( listen_fd = socket(AF_INET, SOCK_STREAM, 0) ) < 0 )
{
message( "Bad socket" );
return 0;
}
/* Riempio la struttura sockaddr_in */
memset(&my_struct,0,sizeof(my_struct));
my_struct.sin_family = AF_INET;
my_struct.sin_port = htons( DCC_PORT );
my_struct.sin_addr.s_addr = htonl( INADDR_ANY );
/* Associo il socket alla struttura */
if( bind( listen_fd, (SA *) &my_struct, sizeof(my_struct) ) < 0 )
{
message( " Error returnd by bind " );
return 0;
}
/* Si mette in ascolto */
if( listen( listen_fd, 1 ) < 0 )
{
message( "Error returned by listen " );
return 0;
}
/* Aspetta una connessione ( in teoria potrebbe anche non rientrare ) */
if( (sock_fd = accept( listen_fd, (SA *) NULL, NULL ) ) < 0 )
{
message( "Error returned by accept " );
return 0;
}
/* CONNESSIONE RIUSCITA */
/* Chiudo il socket che era in ascolto */
close(listen_fd);
file_to_send = (char *)malloc(strlen(&pathfile[n]) + EXST);
strcpy(file_to_send, &pathfile[n]);
// strcat(file_to_send, ".cry");
/* Apre il file criptato */
if( (fp = fopen( file_to_send, "rb" ) ) == NULL )
{
message( " Error while reopening encrypted file " );
return 0;
}
/* Invia il file */
while( !feof(fp) )
{
len_buf = fread( buffer, SIZE_BUF, 1, fp );
write( sock_fd, buffer, len_buf );
}
/* Chiudo il file */
fclose(fp);
return 1;
}
In pratrica la connessione avviene..... ma il lato che invia sembra che stacchi immediatamente la connessione.Qualcuno puoi aiutarmi?! Spero che aver pastato tutto questo codice non sia un problema.
Regards
Emitrax