Reading a file in 1-kilobyte chunks...
How do I read a file in 1 KB chunks and send them to a client? Here's the server:
Code:
SOCKET s = accept(Socket, NULL, NULL);
if (s != SOCKET_ERROR)
{
char filename[MAX_PATH] = "";
recv(s, filename, MAX_PATH, 0);
if (filename)
{
FILE *f = fopen(filename, "rb");
if (f)
{
char buf[1024] = "";
while (feof(f) == 0)
{
fread(buf, 1024, 1, f);
send(s, buf, (int) strlen(buf), 0);
}
}
fclose(f);
}
closesocket(s);
}
And here's the client:
Code:
char filename[MAX_PATH] = "OMGWTF.txt";
send(Socket, filename, MAX_PATH, 0);
int len;
char buf[1024];
FILE *f = fopen(filename, "wb");
do
{
len = recv(Socket, buf, 1024, 0);
fwrite(buf, len, 1, f);
} while (len != 0);
fclose(f);
The received file is about 2 times larger than the original. What's wrong?