FTP using c#.net 2010

Shipper (file transfer) project is developed in c#.net (vs 2010).it use the file transfer protocol to send the file from one computer to another computer.as a small project it is able to send only limited size of file.
it is combination of two project one is shipper sever and other one is Shipper client. First discus the Shipper server part.


You can download this project from: http://www.4shared.com/file/4ha5NFj0/networking_project.html

Shipper Server

In shipper sever has two mode one is send file and second is receive file.

Send file mode: in this mode server will read to send the file to client.


In Receive mode: Server is ready to receive the file from client.

But the question is how?

Ans is when server is set to send mode first select the file which is send to client and give client IP address and port number.

To send the files then start the server.

Same steps will be follow when we receive the file from client but u select the path where the file will store.

Screen Shot:

Welcome screen:

Server screen


For sending the file: we declare these namespace at the top of the project file

<br />
using System.Net;<br />
using System.Net.Sockets;<br />
using System.IO;<br />
try<br />
{<br />
IPEndPoint ipEnd;<br />
Socket sock;<br />
ipEnd = new IPEndPoint(IPAddress.Any, Convert.ToInt32( txt_port.Text));<br />
sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);<br />
sock.Bind(ipEnd);<br />
msgsender = &quot;Starting...&quot;;<br />
sock.Listen(100);<br />
msgsender = &quot;Running and waiting to Send  The file.&quot;;<br />
clientSock = sock.Accept();<br />
string fileName = Path.GetFileName(dlg_open.FileName);<br />
string filePath = Path.GetDirectoryName(dlg_open.FileName);<br />
byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);<br />
if (fileNameByte.Length &gt; 850 * 1024)<br />
{<br />
lst_sserver.Items.Add(&quot;File size is more than 850kb, please try with small file.&quot;);<br />
return;<br />
}<br />
byte[] fileData = File.ReadAllBytes(filePath + &quot;\\&quot; + fileName);<br />
byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];<br />
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);<br />
msgsender = &quot;Sending data...&quot;;<br />
fileNameLen.CopyTo(clientData, 0);<br />
fileNameByte.CopyTo(clientData, 4);<br />
fileData.CopyTo(clientData, 4 + fileNameByte.Length);<br />
clientSock.Send(clientData);<br />
clientSock.Close();<br />
msgsender = &quot;File send.&quot;;<br />
msgsender = &quot;Connection close.&quot;;<br />
}<br />
catch (Exception ex)<br />
{<br />
MessageBox.Show(&quot;Unable to send the file.&quot; + ex.Message);<br />
}<br />


“IPEndPoint” Represents a network endpoint as an IP address and a port number. It’s come under the System.Net. And “Socket” initializes a new instance of the Socket class using the specified value.

Sock.bind(ipend) is use to bind the connection. After the binding the connection set the listing state of socket which is 100. After that select the file and file file path get the file size in bytes and check if it is more than 850 kb then display the error message if it not then send the file to client data. Server socket and client socket number will be same when the server is sending the file or receiving the file from client it’s the short description of the server part code.

Shipper Client

In shipper client it has two parts one is download and other one is uploading. When client is uploading the file it selects the file and give server IP address and port number then click the upload button.when it want to download a file it select the path where the file will be store.

Screen shot

Client screen


Download part:

<p>lst_client.Items.Add(&quot;Buffering ...&quot;);</p>
<p>IPAddress[] ipAddress = Dns.GetHostAddresses(txt_ipaddress.Text);</p>
<p>IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], Convert.ToInt32(txt_port.Text));</p>
<p>Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);</p>
<p>lst_client.Items.Add( &quot;Connection to server ...&quot;);</p>
<p>byte[] clientData = new byte[1024 * 5000];</p>
<p>string receivedPath = dlg_folderbrowser.SelectedPath;</p>
<p>int receivedBytesLen = clientSock.Receive(clientData);</p>
<p>int fileNameLen = BitConverter.ToInt32(clientData, 0);</p>
<p>string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);</p>
<p>lst_client.Items.Add(&quot;File Reciving...&quot;);</p>
<p>BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + &quot;\\&quot; + fileName, FileMode.Append));</p>
<p>bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);</p>
<p>lst_client.Items.Add(&quot;File Recived. from:&quot; + txt_ipaddress.Text + &quot;.&quot;);</p>
<p>catch (Exception ex)</p>
<p>MessageBox.Show(&quot;Unable to recive the file.&quot; + ex.Message, &quot;Shipper Client&quot;, MessageBoxButtons.OK, MessageBoxIcon.Error);</p>

Upload part:

<p>IPAddress[] ipAddress = Dns.GetHostAddresses(txt_ipaddress.Text);</p>
<p>IPEndPoint ipEnd = new IPEndPoint(ipAddress[0], Convert.ToInt32(txt_port.Text));</p>
<p>Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);</p>
<p>string fileName = dlg_open.FileName;</p>
<p>string filePath = &quot;&quot;;</p>
<p>fileName = fileName.Replace(&quot;\\&quot;, &quot;/&quot;);</p>
<p>while (fileName.IndexOf(&quot;/&quot;) &gt; -1)</p>
<p>filePath += fileName.Substring(0, fileName.IndexOf(&quot;/&quot;) + 1);</p>
<p>fileName = fileName.Substring(fileName.IndexOf(&quot;/&quot;) + 1);</p>
<p>byte[] fileNameByte = Encoding.ASCII.GetBytes(fileName);</p>
<p>if (fileNameByte.Length &gt; 850 * 1024)</p>
<p>lst_client.Items.Add(&quot;File size is more than 850kb, please try with small file.&quot;);</p>
<p>lst_client.Items.Add(&quot;Buffering ...&quot;);</p>
<p>byte[] fileData = File.ReadAllBytes(filePath + fileName);</p>
<p>byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];</p>
<p>byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);</p>
<p>fileNameLen.CopyTo(clientData, 0);</p>
<p>fileNameByte.CopyTo(clientData, 4);</p>
<p>fileData.CopyTo(clientData, 4 + fileNameByte.Length);</p>
<p>lst_client.Items.Add(&quot;Connection to server ...&quot;);</p>
<p>lst_client.Items.Add(&quot;File sending...&quot;);</p>
<p>lst_client.Items.Add(&quot;File transferred.&quot;);</p>
<p>catch (Exception ex)</p>
<p>MessageBox.Show(&quot;File Sending fail.&quot; + ex.Message,&quot;Shipper Client&quot;,MessageBoxButtons.OK,MessageBoxIcon.Error);</p>

Description: when we download some file from server it connects the server by giving the server IP address and its port number. “BinaryWriter” is responsible for receiving the file from server first it bind the connections using the IP address and port number. And receive the file from server byte by byte.
When we upload the file it will be done in three steps. First file length will be send to server, second file name will copy to server, it will create the file on server then the data will upload to the server using the “fileData.CopyTo()”.

2 thoughts on “FTP using c#.net 2010

  1. Very good article. I have two questions: Will this application be able to support multiple clients connecting to the server simultaneously and how will I be able to transfer larger files?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.