1. Idea: In order to achieve a variety of file size can be sent and received, can set the buffer loop send and receive, and record the location of the file read, if the number of bytes read into the buffer is less than the size of the buffer, then the file is sent, exit the Send loop, close the connection. And the CSocket is blocked, the receiving end can receive all the content sent by the sender until the sending end connection is closed and the receiving end exits the receiving loop.
Loop Send File
2. Main code
Send side:
Do
{
File. Seek (Step,cfile::begin);
Over=file. Read (buffer,buffersize);//File reads buffer
Err=sockclient.send (Buffer,over);
if ( buffersize>over)
Break the number of bytes in the//buffer is less than the buffer, that is, the file is less than one buffer, exit
Step+=err; The next time you start reading a file from step
}while (true);
Sockclient.close ();
Receiving end:
while ( true)
{
Finish=filelisten.receive (buffer,buffersize);
if (0==finish)
break;//Client send connection is closed, send complete
File. Write (Buffer,finish);
step+=finish;//where to write files
}
Note: To set the buffer size reasonably, this method is less efficient, is not the multi-thread can speed up?
A method of multi-thread transfer file for socket LAN