So I have a client and a server. The client receives the names of the source and destination files from the command line. The server receives the destination file's name and checks if it already exists and if it doesn't he sends a confirmation to the client. The client then proceeds to transfer the file's data to server and the server then creates a new file with that content.
My problem is that:
1)The destination file isn't complete(Happens with bigger files), even though its size is the same as the source's size.
2)Some characters in the dest file are "transformed" i.e. they aren't the same as the ones in the source.
I attached both the server and client files. Here I'll just show the parts where I'm actually transferring the data from the client to the server.
Client sends data:
Code:
//read and send source contents
while((res = read(fd, cont, sizeof(cont))) != 0) {
if(res == -1) {
perror("read");
exit(EXIT_FAILURE);
}
//printf("%s\n", cont);
printf("res: %d\n", res);
//send source content
if(write(sd, cont, sizeof(cont)) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
}
Server reads and writes to file:
Code:
int k = 0;
do {
if((res = read(nsd, cont, sizeof(cont))) != 0) {
if(res == -1) {
perror("read");
exit(EXIT_FAILURE);
}
if(k == 0) {
cont2 = malloc(size);
strcpy(cont2, cont);
k++;
}
else
strcat(cont2, cont);
}
}
while(res!=0); //while sum of bytes read < source size, keep reading
// write data to dest
if(write(fd, cont2, sizeof(char)*size) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
What am I dealing with?
PS: Client's first parameter is the source then the destination file.
PPS: I'm sort of supposed to send the source's contents over in blocks(say 1024 bytes long). That's probably the reason I'm making life difficult for myself.