diff --git a/client_ftp_functions.c b/client_ftp_functions.c index 2831072..4d4d061 100644 --- a/client_ftp_functions.c +++ b/client_ftp_functions.c @@ -192,7 +192,16 @@ void command_get(struct packet* chp, struct packet* data, int sfd_client, char* data = htonp(chp); if((x = send(sfd_client, data, size_packet, 0)) != size_packet) er("send()", x); - receive_file(chp, data, sfd_client, f); - fclose(f); + if((x = recv(sfd_client, data, size_packet, 0)) <= 0) + er("recv()", x); + chp = ntohp(data); + if(chp->type == INFO && chp->comid == GET && strlen(chp->buffer)) + { + printf("\t%s\n", chp->buffer); + receive_file(chp, data, sfd_client, f); + fclose(f); + } + else + fprintf(stderr, "Error getting remote file.\n"); } diff --git a/file_transfer_functions.c b/file_transfer_functions.c index 39aca93..94d0fd2 100644 --- a/file_transfer_functions.c +++ b/file_transfer_functions.c @@ -23,17 +23,21 @@ void send_TERM(struct packet* hp, struct packet* data, int sfd) void send_file(struct packet* hp, struct packet* data, int sfd, FILE* f) { int x; - strcpy(hp->buffer, "test"); - hp->type = DATA; - data = htonp(hp); - if((x = send(sfd, data, size_packet, 0)) != size_packet) - er("send()", x); - send_EOT(hp, data, sfd); + while(!feof(f)) + { + fread(hp->buffer, 1, LENBUFFER, f); + data = htonp(hp); + if((x = send(sfd, data, size_packet, 0)) != size_packet) + er("send()", x); + } } void receive_file(struct packet* hp, struct packet* data, int sfd, FILE* f) { int x; + if((x = recv(sfd, data, size_packet, 0)) <= 0) + er("recv()", x); + hp = ntohp(data); while(hp->type == DATA) { fwrite(hp->buffer, 1, strlen(hp->buffer), f); diff --git a/server_ftp_functions.c b/server_ftp_functions.c index a5da18a..3f81049 100644 --- a/server_ftp_functions.c +++ b/server_ftp_functions.c @@ -50,15 +50,20 @@ void command_ls(struct packet* shp, struct packet* data, int sfd_client, char* l void command_get(struct packet* shp, struct packet* data, int sfd_client) { - FILE* f = fopen(shp->buffer, "rb"); + int x; + FILE* f = fopen(shp->buffer, "rb"); // Yo! shp->type = INFO; shp->comid = GET; strcpy(shp->buffer, f ? "File found; processing" : "Error opening file."); + data = htonp(shp); if((x = send(sfd_client, data, size_packet, 0)) != size_packet) er("send()", x); if(f) + { + shp->type = DATA; send_file(shp, data, sfd_client, f); - fclose(f); + fclose(f); + } send_EOT(shp, data, sfd_client); }