4
Answers

How to reuse established connections

Simon Dahlquist

Simon Dahlquist

14y
3.6k
1

Hi!
I've used this example to create a very simple FTP client which purpose is just to download all files from an FTP server and after succeded download, delete this files from the server.
public static bool DisplayFileFromServer(Uri serverUri)
{
    // The serverUri parameter should start with the ftp:// scheme.
    if (serverUri.Scheme != Uri.UriSchemeFtp)
    {
        return false;
    }
    // Get the object used to communicate with the server.
    WebClient request = new WebClient();

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential ("anonymous","[email protected]");
    try
    {
        byte [] newFileData = request.DownloadData (serverUri.ToString());
        string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
        Console.WriteLine(fileString);
    }
    catch (WebException e)
    {
        Console.WriteLine(e.ToString());
    }
    return true;
}
 
I've implemeted the multiple files download such as first getting the files to be downloaded into a List<string> fileList
I've added the
foreach (string file in fileList)
before the try { byte[] ... section byt this creates a new connection to the ftp server for each file.
Does anyone know how to create this multiple file download so that the established connection is reused for each file so that there will just be one used connection to the ftp server?
Is there possible to change the current uri string in an established connection?
I would be glad for any suggestions how to perform this tiny ftp client.
/Daysim
Answers (4)