Hello. I'm trying to send a file to a particular socket using C; here's the code:
int send_file(char *filepath,int sock)
{
FILE *fp=fopen(filepath,"rb");
void *buff=malloc(2000);
int i=1;
while(i)
{
int bytes_read=fread(buff,1,2000,fp);
i=!(feof(fp));
int bytes_sent=0;
while(bytes_sent<bytes_read)
{
int n=send(sock,buff+bytes_sent,bytes_read-bytes_sent,0);
if(n==-1)
return -1; //failure
bytes_sent+=n;
}
}
fclose(fp);
free(buff);
return 0;
}
When I run this program and try to view the text file in Firefox at http://127.0.0.1:8080/ , a part of the file is cut off from the end if the file size is over 2000 bytes. If I send a picture, only 3/4th of the picture loads (cut off from the bottom).
The function always returns 0 to the caller though. Where does the last chunk of bytes it send()s disappear? Do I need to flush some stream before returning?
Thank you
EDIT: This is a snippet from my main() function:
send_file(filepath, sock);
close(sock);
return 0;
}