recv() returns the number of bytes actually read into the buffer so you should be able to ignore anything else.
As far as I know flush only works on output streams??
Thanks for your interest in my post.
But can you state clearly what you mean by output stream, and does sockets in C++ does not require flushing? Actually, initially i was transferring a file from Client to Server, and even after sending the last set of bytes of file to the server, the server was still waiting at the recv() function.
A snippet of my code is here :
void getFile(SOCKET c_socket)
{
char *memblock;
int num_of_bytes,file_buf = 100000; //any user specific number of bytes
memblock = new char [file_buf];
//dest file
ofstream dest("c:\\dest\\umg.rar",ios::app | ios::binary);
while(true)
{
//reading "num_of_bytes" byte(s) from sourcefile
num_of_bytes = recv( c_socket, memblock, file_buf, 0 );
//writing "num_of_bytes" byte(s) to destination file
dest.write(memblock,num_of_bytes);
//last bunch of bytes recieved
if(num_of_bytes == 0) break;
}
//closing source and destination files.
dest.close();
printf("\n The file was successfully copied.\n");
delete memblock;
//system("pause");
return;
}
here the while loop never terminates and the control again reaches to the recv() function.
When i see the file in this case(i.e. when the control is inside the loop and the program still running) it is not copied at the server and some bytes are remaining. And when i closed the apllication, the remaining bytes were copied to the file.
One of my friend told me that some bytes are still in the buffer which are copied to the file and when i close the application, those remaining bytes from the buffer are copied to the file. To eliminate this he told me to use this condition instead :
//last bunch of bytes recieved
if(num_of_bytes < file_buf) break;
so this worked fine. But he still says that i should have flushed the socket buffer instead, either before sending the bytes or after receiving.
Please Help.
Pawan