I'm creating a file server...

The problem is when i reach end of file. In server's side, the message is Connection reset by peer, and the client create a file whith a wrong size (multiple of buffer size).
I want to transfer any types of file. I am using a tar.gz with 20k for tests..

Server's side.
Code:
char buffer[10000];
/* the filesize */
bytes_write = write(data->client_fd, &filesize, sizeof(filesize));

while(!feof(fd)){
	if( fgets( out_buffer, BUFFER, fd ) != NULL )
		bytes_write = write(data->client_fd, out_buffer, BUFFER);
		if (bytes_write < 0){
			perror("\nSending...\n");
		}
}
Client's side:
Code:
char buffer[10000];
/* receive the file size */
bytes_read = read(socket_fd, &filesize, sizeof(filesize));
tmp = 0;
while(tmp <= filesize){
	bytes_read = read(socket_fd, buffer, BUFFER);
	if (bytes_read < 0){
		perror("\nReceiving data\n");
	}
	/* write in the output file */
	fwrite(buffer, 1, bytes_read, fd);
	tmp+= bytes_read;
}
I see the problem in tmp because it will be a multiple of buffer size. But, how i can do this correctly ?