C # technology to share "socket-based breakpoint continuation function"

Source: Internet
Author: User

Recently developed a remote disaster recovery transmission tool, weekly from Guangzhou server synchronization 5g size database backup files to Beijing server.

The first version of the Transport tool developed very simple, two window services, one responsible for sending, the other responsible for receiving, using the socket to establish a TCP link, on the test server to transfer 10g size files no problem.

However, after deployment to the production environment, each transmission of about 3g of data is interrupted, the analysis is due to network instability, so the transfer tool needs to add a breakpoint, when the transmission unexpected interruption, you can automatically connect, and complete the last outstanding transfer.

The principle of the continuation of the breakpoint is very simple, that is, the partition needs to transfer the file, each time a small piece of data transmission, and with the location and size of the data information, the server successfully received data, then continue the next piece of data transmission, or repeat the transmission of the last piece of data until successful.

This is actually to add a pause function for your transfer function, the network outage I paused the transfer, the network resumed after the transmission continues.

Now that we've figured out the principle, let's start writing code, and here's the core code.

Encapsulates a class containing a field that records the status of a transfer: filename filename, FileSize file size, size of packagesize packet, total number of packagecount transfers, Index current transfer location. Each time the packet is sent, with this information, even if the unexpected interruption, re-connected, you can easily determine the progress of the transmission.

 Public classbreakpointpost{ Public stringFileName {Get;Set; } Public LongFileSize {Get;Set; } Public Longpackagesize {Get;Set; } Public intPackagecount {Get;Set; } Public intIndex {Get;Set; }}

Get the number of files transferred

privatestaticint GetFilePackageCount(longlong packageSize){    intcount0;    if0)        count1;    else        count = Convert.ToInt32(fileSize / packageSize);    returncount;}

Block Read file

Private Static byte[]FileRead(stringPathintIndexLongSize) {byte[] result =NULL;LongLength = (Long) Index * (Long) size + size;using(FileStream stream = file.openread (path)) {if(Length > stream.) Length) result =New byte[Stream. Length-((Long) Index * (LongSize)];Elseresult =New byte[Size]; Stream. Seek ((Long) Index * (Long) size, seekorigin.begin); Stream. Read (Result,0, result.    Length); }returnResult;}

chunked Accept File

privatestaticvoidFileWrite(stringintlongintbyte[] data){    using (System.IO.FileStream stream = System.IO.File.OpenWrite(path))    {        stream.Seek((long)index * (long)packageSize, System.IO.SeekOrigin.Begin);        0, receiveSize);        stream.Flush();    }}

The above is the core code, you can according to these code, write their own breakpoint continuation function.
Of course, the complete code example is certainly not necessary, the following two versions are downloaded on demand.
1. Console version (download with surprise)
2. Windows Service Edition (production version)
3, like with GitHub children's shoes can also point me fork

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C # technology to share "socket-based breakpoint continuation function"

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.