Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

C# FTP client using TcpClient class -- please help

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
almurph@altavista.com - 05 Mar 2008 16:43 GMT
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.
parez - 05 Mar 2008 16:55 GMT
On Mar 5, 11:43 am, "almu...@altavista.com" <almu...@altavista.com>
wrote:
> Folks,
>
[quoted text clipped - 14 lines]
> 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%20client%20in%20.net
almurph@altavista.com - 05 Mar 2008 16:59 GMT
> On Mar 5, 11:43 am, "almu...@altavista.com" <almu...@altavista.com>
> wrote:
[quoted text clipped - 32 lines]
>
> - Show quoted text -

Can you help me please, to get me started?

Thanks,
Al.
The stupid one.
parez - 05 Mar 2008 17:03 GMT
On Mar 5, 11:59 am, "almu...@altavista.com" <almu...@altavista.com>
wrote:

> > On Mar 5, 11:43 am, "almu...@altavista.com" <almu...@altavista.com>
> > wrote:
[quoted text clipped - 38 lines]
> Al.
> The stupid one.

This mite be a good starting  point

http://www.ondotnet.com/pub/a/dotnet/2004/05/10/ftpdotnet.htm
Peter Duniho - 05 Mar 2008 18:41 GMT
> Can you help me please, to get me started?

If you want to implement an FTP client from scratch, this is the place to  
start:
http://www.faqs.org/rfcs/rfc765.html

Otherwise, you probably would be better off using the built-in support for  
FTP.  Here's a starting point for that:
http://msdn2.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

Pete
Arne Vajhøj - 06 Mar 2008 02:40 GMT
>     I'm trying to use C# to connect to an FTP server to upload some
> files. I'm calling:
[quoted text clipped - 9 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.