C # UDP (socket) asynchronous file transfer (1)

Source: Internet
Author: User
Sending files with scoket is not very easy to handle, and online examples are also very simple. I am going to write a perfect example. This is the beginning, in the future, we will make modifications based on this example to test the completeness of multi-thread transmission, breakpoint transmission, and file transmission.

In this example, the File Sending management class (sendfilemanager), the file receiving management class (receivefilemanager), the File Sending class (udpsendfile), and the file receiving Class (udprecievefile) are defined respectively ), so that they can be used as simple as possible. In this example, both sending and reading and writing files are based on Asynchronization, enabling multipart transmission of large files. In this example, a file sender and a file receiver are provided. The preceding classes are used to send and accept files. By default, the accepted file is placed in the root directory of the accepted file. The file name starts with an underscore.

Below are the sending and receiving files:

 


In the previous article C # UDP (socket) asynchronous file transfer (1), the basic file transmission is realized, but the information in the transmission process is invisible, this article makes some improvements to the previous article, and can understand the transmitted information (Added with log). It also adds the function of receiving or rejecting received files.

In the previous article, the File Sending class (udpsendfile) and file receiving Class (udprecievefile) were directly sent and received using udpclient. Now, I have added a new class udppeer, implements basic asynchronous transmission and data receiving methods, and defines an interface idatacell as a data transmission unit. It contains two methods: tobuffer and frombuffer, all sent data can be inherited. Udpclient In the File Sending class (udpsendfile) and file receiving Class (udprecievefile) is replaced by udppeer.

Log events are added to the File Sending class (udpsendfile) and file receiving Class (udprecievefile) so that we can understand the file transmission process and the file receiving Class (udprecievefile) added the requestsendfile event. When receiving a File Sending request, you can accept or reject the file. Next let's take a look at the new program.

Start listening for received files:

Next, this article goes on to the previous article C # UDP (socket) asynchronous file transfer (2). On the basis of the previous article, it implements Multi-file transmission and integrity verification after file transmission is completed.

To transfer multiple files, you must improve the data format of the files to be sent in article (2). You must add the ID of the file to which each data is sent, in this way, we can process the data correctly when the file is received. In specific implementation, I use the MD5 value of the file as the data identification of different files. In this way, when we receive the data, we first determine which file the data belongs to based on the MD5 value, and then proceed with the corresponding processing.

To transfer multiple files, in the file sending class (udpsendfile) and file receiving Class (udprecievefile), you cannot define only one sendfilemanager and receivefilemanager to manage the sending and receiving files, we need to create a corresponding sendfilemanager and receivefilemanager for each file for management. In the udpsendfile and udprecievefile, a dictionary <tkey, tvalue> is used to store the management object corresponding to the file. tkey is the MD5 of the file, tvalue is the class object for sending and receiving files. When receiving the data, we will find the corresponding file management object based on the MD5 value in the data and perform corresponding operations. After the file is sent or transmitted, the corresponding objects are removed from dictionary <tkey, tvalue> and the resources occupied by them are cleared.

After receiving the file, I have defined an event receivefilecomplete in receivefilemanager. This event will perform MD5 verification on the file and notify us of the result, we can see whether the file is completely transmitted.

Now let's take a look at the sum of Multi-file transmission and MD5 verification:


 

Send a file to start listening:


 

Receive File Sending request:


Agree to receive and start to accept the file:

After receiving the consent message, the sender starts to send the file:

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.