File Transfer Program
File Transfer Program
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:
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....";
}
}
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
After Selecting the Target Loc ation, in the Client c lick the Send button; the file will be sent to
I Hope you will understand the File Transfer operation in a C#.Net Windows applic ation