Sei sulla pagina 1di 6

Source : C# Corner (www.c-sharpcorner.

com) Print
ARTICLE

How to easily send files (inc luding Audio, Video, doc or any type of file) from Client to Server.
It is necessary to spec ify the server's "Computer Name" in the Tc pClient space like:
Tc pClient c lient = new Tc pClient("SwtRascal", 5055);
In this the Tc pClient specified the Computer Name as "SwtRasc al".
First we have to design a form for the client suc h as:

And plac e an OpenFileDialog from the ToolBox-> Dialogs-> OpenFileDialog c ontrol.


Then Double-Clic k the form; the c oding page will open; in that spec ify the following namespac es:
using System.Net.Sockets;
using System.IO;
Then write c ode for the button1 (Browse) and the button2 (Send) as in the following.
For example:
CLIENT PROGRAM:
using
using
using
using
using
using
using
using
using

System;
System.Collec tions.Generic ;
System.ComponentModel;
System.Data;
System.Drawing;
System.Text;
System.Windows.Forms;
System.Net.Sockets;
System.IO;

namespac e filee
{
public partial c lass Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string n;
byte[] b1;
OpenFileDialog op;
private void button1_Clic k(object sender, EventArgs e)
{
op = new OpenFileDialog();
if (op.ShowDialog() == DialogResult.OK)
{
string t = textBox1.Text;
t = op.FileName;
FileInfo fi = new FileInfo(textBox1.Text = op.FileName);
n = fi.Name + "." + fi.Length;
Tc pClient c lient = new Tc pClient("SwtRasc al", 5055);
StreamWriter sw = new StreamWriter(c lient.GetStream());
sw.WriteLine(n);
sw.Flush();
label1.Text = "File Transferred....";
}
}
private void button2_Clic k(object sender, EventArgs e)
{
TcpClient c lient = new TcpClient("SwtRascal", 5050);
Stream s = c lient.GetStream();
b1 = File.ReadAllBytes(op.FileName);
s.Write(b1, 0, b1.Length);
client.Close();
label1.Text = "File Transferred....";
}
}

For Server : Open a new C# Windows Applic ation form.


Create and design a Form for Server like:

Then Place a folderBrowserDialog from the ToolBox->Dialogs-> folderBrowserDialog.


After Designing the Form, Double-Clic k the form; the coding page will open; in that specify the
following namespac es:
using System.Net.Soc kets;
using System.IO;
using System.Net;
Then write c ode for the button1 (Browse) and for the Form Load func tion, suc h as in the
following.
If you want to spec ipy an IP Address for the system then inc lude code suc h as:
Instead of using the c ode TcpListener list = new Tc pListener(port1);
It only spec ifies the port address.
Use this c ode.
It specifies the IP Address and Port.
IPAddress localAddr = IPAddress.Parse("192.168.1.20");
Tc pListener list = new TcpListener(loc alAddr, port);
Below the Comment line shows the ipaddress option..
For example:
SERVER PROGRAM:
using
using
using
using
using
using
using
using
using
using

System;
System.Collec tions.Generic ;
System.ComponentModel;
System.Data;
System.Drawing;
System.Text;
System.Windows.Forms;
System.Net.Soc kets;
System.IO;
System.Net;

namespac e filee
{
public partial c lass Form2 : Form
{
public Form2()
{
InitializeComponent();
}
string rd;
byte[] b1;
string v;
int m;
Tc pListener list;
Int32 port = 5050;
Int32 port1 = 5055;
//IPAddress localAddr = IPAddress.Parse("192.168.1.20");
private void Browse_Clic k(objec t sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog1.Selec tedPath;
//Tc pListener list = new TcpListener(loc alAddr,port1);
list = new Tc pListener(port1);
list.Start();
Tc pClient c lient = list.Acc eptTc pClient();
Stream s = client.GetStream();
b1 = new byte[m];
s.Read(b1, 0, b1.Length);
File.WriteAllBytes(textBox1.Text + "\\" + rd.Substring(0,

rd.LastIndexOf('.')),

b1);
list.Stop();
c lient.Close();
label1.Text = "File Received......";
}
}
private void Form2_Load(objec t sender, EventArgs e)
{
//TcpListener list = new Tc pListener(localAddr, port);
TcpListener list = new Tc pListener(port);
list.Start();
TcpClient c lient = list.Ac ceptTcpClient();
MessageBox.Show("Client trying to c onnect");
StreamReader sr = new StreamReader(client.GetStream());
rd = sr.ReadLine();
v = rd.Substring(rd.LastIndexOf('.') + 1);
m = int.Parse(v);
list.Stop();
client.Close();
}
}

After Designing the Forms for Client and Server, run the Server first and after that run the Client.
Output: Client Selec ting the Sourc e File

For Server: Selec ting the Target location

After Selecting the Target Loc ation, in the Client c lick the Send button; the file will be sent to

the target location.

I Hope you will understand the File Transfer operation in a C#.Net Windows applic ation

Thank you for using C# Corner

Potrebbero piacerti anche