Simple File Transfer Part 1
<< Back to the .Networking page
In this tutorial you will learn how to transfer files by uploading to and downloading from the Shared Dictionary.
Download: SimpleFileTransfer1.zip Source: SimpleFileTransfer1Src.zip Unzip all files (executables plus dependancies) into a single folder |
Introduction
Below is the full code required to make a minimal File Transfer program. In this simple program, multiple clients can upload files to the Shared Dictionary. The file last uploaded will be shown in a lable. Only the file shown in the lable can be downloaded (Of couse, you can modify the program so that you can choose the file to download, but for the purpose of this demo, we will leave it as simple as possible).
Setup
Before you begin, make sure you downloaded and setup GroupLab.Networking. Click here for detailed instructions. You should also install GroupLab_Networking_v1_0_2.msi.
Tutorial
1) Create the empty project. Create a C# project (Windows Application). Call it SimpleFileTransfer.
2) ''Add SharedDictionary and Subscription to the Form". Under the GroupLab.Networking tap in the Toolbox, click on the SharedDictionary component and drag an instance onto the form. Do the same for the Subscription component.

3) ''Add OpenFileDialog and SaveFileDialog to the Form". Under the Windowns Forms tap in the Toolbox, click on the OpenFileDialog component and drag an instance onto the form. Do the same for the SaveFileDialog component.
4) ''Add two buttons and a label to the Form". After adding them, change the text and name of the buttons in Properties. Name the buttons to uploadButton and downloadButton. Change the text to Upload and Download accordingly. In the properties of the label, change TextAlign to MiddleCenter and make the text empty.

5) Add code. Right click on the Form and chose View Code.
- Add a path. Add the following line after "using System.Data;"
- Add a variable. Add the following line after "private System.ComponentModel.IContainer components;"
- Add initialization code. Add the following code after "InitializeComponent();" Double tap after "+=" to add event handlers, so that empty functions are added automatically.
this.sharedDictionary1.Url = "tcp://localhost:test";
// open the connection
this.sharedDictionary1.Open();
// signal begining of initialization for subscription1
this.subscription1.BeginInit();
// register the pattern with the shared dictionary server (everything in this case)
this.subscription1.Pattern = "*";
// register the event handler
this.subscription1.Notified += new SubscriptionEventHandler(subscription1_Notified);
// signal the end of initialization
this.subscription1.EndInit();
- Add code to subscription1_Notified. This will get called if a change is occured in the Shared Dictionary.
this.label1.Text = ft.FileName;
- Add code for upload. Click the Design tap and double click the upload button. Add the following code in uploadButton_Click.
{
ft = new FileTransfer(this.openFileDialog1.FileName);
// add the file to the Shared Dictionary
this.sharedDictionary1[ft.Id.ToString()] = ft;
this.label1.Text = this.openFileDialog1.FileName;
}
- Add code for download. Click the Design tap again and double click the download button. Add the following code in downloadButton_Click.
if(this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
ft.SaveAs(this.saveFileDialog1.FileName);
}
6) Compile.
7) Testing. Now open the Dictionary Monitor from the Start button (assuming you have installed GroupLab.Networking by running the .msi file). Go to Start->All Programs->GroupLab->Networking->Dictionary Monitor. Click Open. In the popup window, the default Address should be "tcp://localhost:test". If not, type it in. Click Open. When you upload a file, it should appear in the Dictionary Monitor. When you click Download, the most recent file will be downloaded. You can open up multiple clients to upload and download files.
