Reputation: 4510
I am trying to implement a distributed p2p file sharing system where a peer can both send and download files from other peers. But, I am having some trouble downloading and saving the file to a directory.
Here is where I send the requested file
while(!feof(requestedfile)) {
bytes_read = fread(buf, 1, sizeof(buf), requestedfile);
send(clientSock, buf, bytes_read, 0);
}
When I try to download, I do the following
while(recv(clientSock, currentLinePointer, 1, 0) != 0) {
currentLinePointer++;
}
I am new to C, but I know I have 2 issues
Upvotes: 0
Views: 1378
Reputation: 27924
recv()
returns the number of bytes received, 0 if connection is closed and -1 on error. That basically answers your question.
You should also keep track of the value returned by send()
. It returns the number of bytes sent, or at last put in buffer for sending. It will not always send everything you tell it to if the buffer is full or the connection is lost. Keep track of its return value to control the send buffer usage and avoid gaps in the file being transfered. You may have to perform a new send()
call to send the part of the data it refused to in a previous call.
Upvotes: 0
Reputation: 281
I suggest you read 4096 bytes at once since this is the standard size of a disk sector nowadays (http://en.wikipedia.org/wiki/Disk_sector):
char* buffer = (char*)malloc(4096), *position = buffer;
while((bytes_read = recv(clientSock, position, 4096, 0) > 0)
{
if(fwrite(position, 1, bytes_read, localFile) != bytes_read)
{
// do some error handling here
break;
}
position += 4096;
}
// and here
free(buffer);
Upvotes: 0
Reputation: 21023
http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx
If no error occurs, recv returns the number of bytes received
You need to know what the return value of recv is and only write that number of bytes to your file.
Upvotes: 1