Seamless transfer of memory stream MemoryStream to FTP stream stream in C #

Source: Internet
Author: User
Tags ftp connection
The earliest use of the queue to hold data, the time node for a one-time read and FTP transmission, but the queue data is too slow, so reuse the memory stream to solve the performance of the data, on the other hand, the data for seamless FTP upload.

1, first set up a memory stream MemoryStream, and build its StreamWriter

MemoryStream data_stream = new MemoryStream ();
StreamWriter data_writer = new StreamWriter (Data_stream);

2, a locked memory stream write to the thread function

int Data_enqueue (string data)
{
    lock (data_monitor)
    {
        try
        {
            data_writer. WriteLine (data);
            Data_writer. Flush ();
            return 1;
        }
        catch (Exception ex)
        {
            class_log.err_write ("[Data_enqueue]" + ex.) message);
            return 0;}}}

3. FTP connection and write the memory stream data directly into the FTP stream

ftpwebrequest ftp = (ftpwebrequest) ftpwebrequest.create (new Uri (URI)); Ftp.
Credentials = new NetworkCredential (Ftp_username, Ftp_password); Ftp.
KeepAlive = false; Specifies that command ftp be executed.
method = WebRequestMethods.Ftp.UploadFile; Specifies the data transfer type FTP.
Usebinary = true; 15 seconds Timeout ftp.

Timeout = 15000; using (Stream Ftp_stream = ftp. GetRequestStream ()) {//Lock Lock (Data_monitor) {//point to Memory stream header Data_stream.
        Position = 0; Data_stream.

        CopyTo (Ftp_stream); Data_writer.
        Close (); Data_stream.

        Close ();
        Data_stream = new MemoryStream ();
    Data_writer = new StreamWriter (Data_stream); } ftp_stream.
Close (); }

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.