A method of multi-thread transfer file for socket LAN

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.