Click here to Skip to main content
16,022,536 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
File Transfer using Socket Application in C# .NET 2.0
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-12 1:12am    
Do you mean FTP or something else?
--SA
Sergey Alexandrovich Kryukov 21-Jan-12 1:14am    
Client, server or both?
--SA

The closest to what you might want (please see my comments to the question, which is not clearly defined) is FTP, see http://en.wikipedia.org/wiki/FTP[^].

If you need to implement the server part, this is more difficult. Please see this CodeProject article:
Advanced FTP Server[^].

I looked at the article before answering to make sure the code for .NET v.2.0 is available.

Client part is much easier. You need to use the class FtpWebRequest. This is a help page for v.2.0: http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest%28v=vs.80%29.aspx[^].

Please see the code sample in this article.

If this is not enough, Google or find some more in CodeProject articles: http://www.codeproject.com/search.aspx?q=FTP&doctypeid=1[^].

You will find a lot.

—SA
 
Share this answer
 
SERVER:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
 
namespace SocketfileServer
{
public partial class Form1 : Form
{
private Thread thrDownload;
private Stream strLocal;
private NetworkStream strRemote;
private TcpListener tlsServer;
private delegate void UpdateStatusCallback(string StatusMessage);
private delegate void UpdateProgressCallback(Int64 BytesRead, Int64 TotalBytes);
private static int PercentProgress;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void btnStart_Click(object sender, EventArgs e)
{
thrDownload = new Thread(StartReceiving);
thrDownload.Start();
}
private void StartReceiving()
{
try
{
string hstServer = Dns.GetHostName();
IPAddress ipaLocal = Dns.GetHostEntry(hstServer).AddressList[0];
if (tlsServer == null)
{
tlsServer = new TcpListener(ipaLocal, Convert.ToInt32(txtPort.Text));
}
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Starting the server...\r\n" });
tlsServer.Start();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has started. Please connect the client to " + ipaLocal.ToString() + "\r\n" });
TcpClient tclServer = tlsServer.AcceptTcpClient();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has accepted the client\r\n" });
strRemote = tclServer.GetStream();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The server has received the stream\r\n" });
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
string FileName = System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize);
strLocal = new FileStream(@"C:\" + FileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
downBuffer = new byte[2048];
bytesSize = strRemote.Read(downBuffer, 0, 2048);
long FileSize = Convert.ToInt64(System.Text.Encoding.ASCII.GetString(downBuffer, 0, bytesSize));
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Receiving file " + FileName + " (" + FileSize + " bytes)\r\n" });
downBuffer = new byte[2048];
while ((bytesSize = strRemote.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgressCallback(this.UpdateProgress), new object[] { strLocal.Length, FileSize });
}
}
finally
{
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"The file was received. Closing streams.\r\n" });
strLocal.Close();
strRemote.Close();
this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] {"Streams are now closed.\r\n" });
StartReceiving();
}
}
private void UpdateStatus(string StatusMessage)
{
txtLog.Text += StatusMessage;
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
if (TotalBytes > 0)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
}
}
private void btnStop_Click(object sender, EventArgs e)
{
strLocal.Close();
strRemote.Close();
txtLog.Text += "Streams are now closed.\r\n";
}
}
}
 

CLIENT:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
 
namespace SocketfileClient
{
public partial class Form1 : Form
{
TcpClient tcpClient;
FileStream fstFile;
NetworkStream strRemote;
public Form1()
{
InitializeComponent();
}
private void ConnectToServer(string ServerIP, int ServerPort)
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect(ServerIP, ServerPort);
txtLog.Text += "Successfully connected to server\r\n";
}
catch (Exception exMessage)
{
txtLog.Text += exMessage.Message;
}
}
private void btnConnect_Click(object sender, EventArgs e)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Disconnected from server.\r\n";
}
private void btnSend_Click(object sender, EventArgs e)
{
if (tcpClient.Connected == false)
{
ConnectToServer(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
if (openFile.ShowDialog() == DialogResult.OK)
{
txtLog.Text += "Sending file information\r\n";
strRemote = tcpClient.GetStream();
byte[] byteSend = new byte[tcpClient.ReceiveBufferSize];
fstFile = new FileStream(openFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader binFile = new BinaryReader(fstFile);
FileInfo fInfo = new FileInfo(openFile.FileName);
string FileName = fInfo.Name;
byte[] ByteFileName = new byte[2048];
ByteFileName = System.Text.Encoding.ASCII.GetBytes(FileName.ToCharArray());
strRemote.Write(ByteFileName, 0, ByteFileName.Length);
long FileSize = fInfo.Length;
byte[] ByteFileSize = new byte[2048];
ByteFileSize = System.Text.Encoding.ASCII.GetBytes(FileSize.ToString().ToCharArray());
strRemote.Write(ByteFileSize, 0, ByteFileSize.Length);
txtLog.Text += "Sending the file " + FileName + " (" + FileSize + " bytes)\r\n";
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = fstFile.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strRemote.Write(downBuffer, 0, bytesSize);
}
txtLog.Text += "File sent. Closing streams and connections.\r\n";
tcpClient.Close();
strRemote.Close();
fstFile.Close();
txtLog.Text += "Streams and connections are now closed.\r\n";
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
}
}
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 27-Jan-12 2:34am    
Added pre tag

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900