C# FTP client using TcpClient class -- please help

A

almurph

Folks,

I'm trying to use C# to connect to an FTP server to upload some
files. I'm calling:

System.Net.Sockets.TcpClient mtTCPClient = new
System.Net.Sockets.TcpClient(url, 21);


The problem is though I know this server accepts a username and
password but I dont know where to put them. I dont see a constructor
for this in the API.

I have that funny feeling that I'm doing something wrong! Or that I'm
missing something. Can anyone help please. Any comments/suggestions/
things that I should know/code-samples much appreciated.

Thanks,
Al.
 
P

parez

Folks,

I'm trying to use C# to connect to an FTP server to upload some
files. I'm calling:

System.Net.Sockets.TcpClient mtTCPClient = new
System.Net.Sockets.TcpClient(url, 21);

The problem is though I know this server accepts a username and
password but I dont know where to put them. I dont see a constructor
for this in the API.

I have that funny feeling that I'm doing something wrong! Or that I'm
missing something. Can anyone help please. Any comments/suggestions/
things that I should know/code-samples much appreciated.

Thanks,
Al.

Hi Al,

After opening the connection to 21 you will have to write code to send
ftp commands (USER,PWD BY etc).
And your class will have the construcotr for taking username password
and stuff..


Or use 3rd party components.

check this out

http://www.google.com/search?sourceid=gmail&q=ftp client in .net
 
A

Arne Vajhøj

I'm trying to use C# to connect to an FTP server to upload some
files. I'm calling:

System.Net.Sockets.TcpClient mtTCPClient = new
System.Net.Sockets.TcpClient(url, 21);


The problem is though I know this server accepts a username and
password but I dont know where to put them. I dont see a constructor
for this in the API.

I have that funny feeling that I'm doing something wrong! Or that I'm
missing something. Can anyone help please. Any comments/suggestions/
things that I should know/code-samples much appreciated.

TcpClient first argument is not an URL, but just a host name.

..NET has built in FTP support since .NET 2.0 - code snippet:

public static void Upload(string localfile, string ftpurl, bool
bin)
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(ftpurl);
req.Method = WebRequestMethods.Ftp.UploadFile;
req.UseBinary = bin;
if(bin)
{
Stream instm = new FileStream(localfile, FileMode.Open,
FileAccess.Read);
Stream outstm = req.GetRequestStream();
byte[] b = new byte[10000];
int n;
while((n = instm.Read(b, 0, b.Length)) > 0)
{
outstm.Write(b, 0, n);
}
instm.Close();
outstm.Close();
}
else
{
StreamReader sr = new StreamReader(localfile);
StreamWriter sw = new StreamWriter(req.GetRequestStream());
string line;
while((line = sr.ReadLine()) != null)
{
sw.WriteLine(line);
}
sr.Close();
sw.Close();
}
FtpWebResponse resp = (FtpWebResponse)req.GetResponse();
Console.WriteLine(resp.StatusCode);
}

If you reall want to implement a FTP client using TcpClient, then
look at http://www.vajhoej.dk/arne/eksperten/div_2007_03/xftp.cs !

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top