Skip to content

Commit

Permalink
send_file() and receive_file() implemented. Client terminates in a Se…
Browse files Browse the repository at this point in the history
…gmentation fault. Commit before tracking error.
  • Loading branch information
rovinbhandari committed Mar 4, 2012
1 parent badbe19 commit 93a0657
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SRCSERVER = .
BIN = .
OBJCLIENT = obj
OBJSERVER = obj
OBJECTSCLIENT = ${OBJCLIENT}/commons.o ${OBJCLIENT}/client_ftp.o
OBJECTSCLIENT = ${OBJCLIENT}/commons.o ${OBJCLIENT}/client_ftp_functions.o ${OBJCLIENT}/client_ftp.o
OBJECTSSERVER = ${OBJSERVER}/commons.o ${OBJSERVER}/server_ftp_functions.o ${OBJSERVER}/server_ftp.o
EXECUTABLECLIENT = ${BIN}/client_ftp.out
EXECUTABLESERVER = ${BIN}/server_ftp.out
Expand Down
5 changes: 4 additions & 1 deletion client_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ int main(int argc, char* argv[])
short int connection_id;
struct packet* chp = (struct packet*) malloc(size_packet); // client host packet
struct packet* data; // network packet
char path[LENBUFFER];
char filename[LENBUFFER];

if((x = sfd_client = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
er("socket()", x);
Expand All @@ -28,6 +30,7 @@ int main(int argc, char* argv[])
set0(chp);
chp->type = REQU;
chp->conid = -1;
strcpy(path, argv[1]);
strcpy(chp->buffer, argv[1]);
printpacket(chp, HP);
data = htonp(chp);
Expand All @@ -42,7 +45,7 @@ int main(int argc, char* argv[])
if(chp->type == INFO)
printpacket(chp, HP);
else if(chp->type == DATA)
receive_file(extract_filename(path), sfd_client,
receive_file(extract_filename(path), sfd_client, chp);
}
while(chp->type != TERM);

Expand Down
7 changes: 6 additions & 1 deletion client_ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
#define IPSERVER "127.0.0.1"
#define ID "CLIENT=> "

void receive_file(char*,
//char* extract_filename(char*, char*);
#define extract_filename(filepath) ((strrchr(filepath, '/') != NULL) ? (strrchr(filepath, '/') + 1) : filepath)

void receive_file(char*, int, struct packet*);


32 changes: 32 additions & 0 deletions client_ftp_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <client_ftp.h>

static size_t size_packet = sizeof(struct packet);

/*
char* extract_filename(char* filename, char* filepath)
{
strcpy(filename, strrchr(filepath, '/') + 1);
}
*/

void receive_file(char* filename, int sfd_client, struct packet* chp)
{
int x;
struct packet* data = (struct packet*) malloc(size_packet);
FILE* fp = fopen(filename, "rb");
while(chp->type == DATA)
{
fwrite(chp->buffer, 1, strlen(chp->buffer), fp);
if((x = recv(sfd_client, data, size_packet, 0)) <= 0)
er("recv()", x);
chp = ntohp(data);
}
if(chp->type == EOT)
fclose(fp);
else
{
fprintf(stderr, "Error occured while writing to file. Exiting...\n");
exit(2);
}
}

2 changes: 1 addition & 1 deletion server_ftp.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void* serve_client(void* info)
{
//show error, send TERM and break
fprintf(stderr, "packet incomprihensible. closing connection.");
send_term(sfd_client, shp);
send_TERM(sfd_client, shp);
break;
}
}
Expand Down

0 comments on commit 93a0657

Please sign in to comment.