Sei sulla pagina 1di 7

29/04/2013

File Transfer Protocol (FTP) Client - CodeProject


9,834,414 members (39,625 online) Sign in

home

articles

quick answers

discussions

features

community

help
Next

Search for articles, questions, tips

Articles General Programming Internet / Network FTP

Article Browse Code Stats Revisions (7) Alternatives Comments & Discussions (19)

File Transfer Protocol (FTP) Client


By SercanOzdemir , 1 Dec 2011
4.92 (9 votes)

About Article
Connect to FTP server, and download, upload, rename, and delete files or directories. Type Licence First Posted Views Downloads Bookmarked Article CPOL 1 Dec 2011 22,495 6,980 56 times

Download FTP client - 430 KB Download File Explorer component - 73.2 KB

C# Windows .NET Dev Intermediate Internet

Introduction
I know there are a lot of FTP client programs available on the Internet. But we also need to understand FTP (File Transfer Protocol)'s substructure. So this Open Source project will lead your way when trying to learn about FTP. This program's interface look like FileZilla. FileZilla is so popular but it has some bugs. It all started when I tried to open my blog. I needed to connect to my server via FTP, send files, download files, etc. So I decided to write my own application to handle all of this. FileZilla is good enough but it's not mine.

Background
Let's see what we know. We know FTP is a standard TCP based network protocol used to transfer files from one host to another host. And it's a client-server architecture.

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

1/7

29/04/2013

File Transfer Protocol (FTP) Client - CodeProject

Top News
10 Pieces of Really Bad Advice (for Computer Scientists)
Get the Insider News free each morning.

Related Videos
FTP programs were based on command-lines. We can still use "cmd.exe" to connect to FTP servers because FTP works with commands. For example, to send a file, we can use "stor" from the command line. In order to do all of these, the FTP server needs to be running and waiting for incoming requests. We can better understand FTP from Wikipedia: "The client computer is able to communicate with the server on port 21 and it's called the control connection. It remains open for the duration of the session, with a second connection, called the data connection either opened by the server from its port 20 to a negotiated client port (active mode) or opened by the client from an arbitrary port to a negotiated server port (passive mode) as required to transfer file data. The control connection is used for session administration (i.e., commands, identification, passwords) exchanged between the client and server using a Telnet-like protocol. For example, "RETR filename" would transfer the specified file from the server to the client. Due to this two-port structure, FTP is considered an out-of-band protocol, as opposed to an in-band protocol such as HTTP."

HTML 5 - 1004 Hosts, FTP, And Domains

Files

CSV

Related Articles
FTP Wanderer - FTP Client using WININET How Proxy Server serves FTP clients? An FTP client library for .NET 2.0 TJFTP - A Visual C++ FTP Client Pause & Resume at Protocol level. (You can implement it in any language) FTP Client Class Multi-threaded Client/Server Socket Class gzTx - A File Transfer Daemon

"The server responds on the control connection with three digit status codes in ASCII with an optional text message, for example "200" (or "200 OK.") means that the last command was successful. The numbers represent the code number and the optional text represents explanations (e.g., <OK>) or needed parameters (e.g., <Need account for storing file>)." So what do we need to do? It's so clear. Send command, receive "OK" command, send data, receive data, and that's all. But first we should prepare the server. FTP servers can be run in "active" or "passive" mode. Active mode is a server based connection and passive is a client based connection. Let's see more. In active connections, clients send the IP and port to the server then the server will try to connect to the client. But it could be denied by the client because of firewalls. We all use antivirus software or Windows Firewalls, right? Now let's see passive mode. In passive connections, the server sends its IP and port to the client with a "PASV" command then the client can try to connect the server with this IP. It's a very usable way to send files. When we try to send a file, we should use the "PASV" mode first. And as you know, most protocols like FTP, HTTP use ASCII characters when trying to get something because it's a global mode. Because it's global, we will use this mode. And you can get the FTP command list from this URL: http://en.wikipedia.org/wiki/File_Transfer_Protocol.

Basics of uploading files to and downloading from an ftp server FTP Client and HttpFileDownloader Components (Controls) SharpBITS.NET - A Wrapper for the BITS API Extracting files from a remote ZIP archive Steganography 17 - FTP Through a Proxy The Ultimate TCP/IP Home Page MyDownloader: A Multi-thread C# Segmented Download Manager A Universal TCP Socket Class for Non-blocking Server/Clients How to write a simple but effective TCP/IP port scanner for Win32 How To Retrieve and Change FTP File Attributes by Using WinInet Functions. Creating an FTP Server in C# with IPv6 Support Simple FTP demo application using C#.Net 2.0

Using the code


Now we are ready to prepare our application. Let's code something useful :) First of all, we need an "Open File dialog" but integrated with our Form.

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

2/7

29/04/2013

File Transfer Protocol (FTP) Client - CodeProject

File Explorer component


We need a file explorer component to see our files to send to the FTP server with our application's interface. Open a new project of type "Windows Forms User Control Library".

It'll look like this. Now we need a treeview. A few buttons. And a search feature.
Collapse | Copy Code

T r e e V i e w . N o d e s . C l e a r ( ) ; T r e e N o d en o d e D=n e wT r e e N o d e ( ) ; n o d e D . T a g=E n v i r o n m e n t . G e t F o l d e r P a t h ( E n v i r o n m e n t . S p e c i a l F o l d e r . D e s k t o p ) ; n o d e D . T e x t=" D e s k t o p " ; n o d e D . I m a g e I n d e x=1 0 ; n o d e D . S e l e c t e d I m a g e I n d e x=1 0 ; T r e e V i e w . N o d e s . A d d ( n o d e D ) ;

Like shown in this code, we should add the main nodes first, "My Documents", "My Computer", etc. Then get the sub directories.
Collapse | Copy Code

s t r i n g [ ]d i r L i s t ; d i r L i s t=D i r e c t o r y . G e t D i r e c t o r i e s ( p a r e n t N o d e . T a g . T o S t r i n g ( ) ) ; A r r a y . S o r t ( d i r L i s t ) ; i f( d i r L i s t . L e n g t h= =p a r e n t N o d e . N o d e s . C o u n t ) r e t u r n ; f o r( i n ti=0 ;i<d i r L i s t . L e n g t h ;i + + ) { n o d e=n e wT r e e N o d e ( ) ; n o d e . T a g=d i r L i s t [ i ] ; n o d e . T e x t=d i r L i s t [ i ] . S u b s t r i n g ( d i r L i s t [ i ] . L a s t I n d e x O f ( @ " \ " )+1 ) ; n o d e . I m a g e I n d e x=1 ; p a r e n t N o d e . N o d e s . A d d ( n o d e ) ; }

You can get the complete code from the link above. We should track the click, down events implemented by the mouse. Now we have a file explorer, and all the needed information about FTP and Visual Studio. First of all, we need to connect to the server. What should we do?
Collapse | Copy Code

F T P S o c k e t=n e wS o c k e t ( A d d r e s s F a m i l y . I n t e r N e t w o r k ,S o c k e t T y p e . S t r e a m ,P r o t o c o l T y p e . T c p ) ; A p p e n d T e x t ( r c h L o g , " S t a t u s:R e s o l v i n gI PA d d r e s s \ n " , C o l o r . R e d ) ; r e m o t e A d d r e s s=D n s . G e t H o s t E n t r y ( S e r v e r ) . A d d r e s s L i s t [ 0 ] ; A p p e n d T e x t ( r c h L o g ," S t a t u s:I PA d d r e s sF o u n d> "+r e m o t e A d d r e s s . T o S t r i n g ( )+" \ n " , C o l o r . R e d ) ; a d d r E n d P o i n t=n e wI P E n d P o i n t ( r e m o t e A d d r e s s ,P o r t ) ; A p p e n d T e x t ( r c h L o g , " S t a t u s:E n d P o i n tF o u n d> "+a d d r E n d P o i n t . T o S t r i n g ( )+" \ n " , C o l o r . R e d ) ; F T P S o c k e t . C o n n e c t ( a d d r E n d P o i n t ) ;

Yes, we need an open socket so connect to our server. Now we can send our commands:
Collapse | Copy Code

A p p e n d T e x t ( r c h L o g ," C o m m a n d:"+m s g+" \ n " ,C o l o r . B l u e ) ; B y t e [ ]C o m m a n d B y t e s=E n c o d i n g . A S C I I . G e t B y t e s ( ( m s g+" \ r \ n " ) . T o C h a r A r r a y ( ) ) ; F T P S o c k e t . S e n d ( C o m m a n d B y t e s ,C o m m a n d B y t e s . L e n g t h ,0 ) ; / / r e a dR e s p o n s e R e a d R e s p o n s e ( ) ;

We send our command to the server and it will answer but in its own language. We need to understand it. The answer include a 3 digit code and an explanation.
Collapse | Copy Code

p r i v a t es t r i n gS p l i t R e s p o n s e ( ) { t r y

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

3/7

29/04/2013
{

File Transfer Protocol (FTP) Client - CodeProject


w h i l e( t r u e ) { B y t e s=F T P S o c k e t . R e c e i v e ( B u f f e r ,B u f f e r . L e n g t h ,0 ) ;/ / N u m b e rO fB y t e s( C o u n t ) S t a t u s M e s s a g e+ =E n c o d i n g . A S C I I . G e t S t r i n g ( B u f f e r ,0 ,B y t e s ) ;/ / C o n v e r tt o i f( B y t e s<B u f f e r . L e n g t h ) / / E n dO fR e s p o n s e b r e a k ;

S t r i n g

} c a t c h ( E x c e p t i o ne x ) { A p p e n d T e x t ( r c h L o g ," S t a t u s:E R R O R ."+ e x . M e s s a g e +" \ n " ,C o l o r . R e d ) ; F T P S o c k e t . C l o s e ( ) ; r e t u r n" " ; }

} s t r i n g [ ]m s g=S t a t u s M e s s a g e . S p l i t ( ' \ n ' ) ; i f( S t a t u s M e s s a g e . L e n g t h>2 ) S t a t u s M e s s a g e=m s g [ m s g . L e n g t h-2 ] ; / / R e m o v eL a s t\ n e l s e S t a t u s M e s s a g e=m s g [ 0 ] ; i f( ! S t a t u s M e s s a g e . S u b s t r i n g ( 3 ,1 ) . E q u a l s ( "" ) ) r e t u r nS p l i t R e s p o n s e ( ) ; f o r( i n ti=0 ;i<m s g . L e n g t h-1 ;i + + ) A p p e n d T e x t ( r c h L o g ," R e s p o n s e:"+m s g [ i ]+" \ n " ,C o l o r . G r e e n ) ; r e t u r nS t a t u s M e s s a g e ;

Yes, that's all .Now we can download, upload, rename, or delete everything :)

List of FTP commands


For easy reference I am including the command list from Wikipedia: Command ABOR ACCT ADAT ALLO APPE AUTH CCC CDUP CONF CWD DELE ENC EPRT EPSV FEAT HELP LANG RFC 2640 RFC 2228 RFC 2428 RFC 2428 RFC 2389 RFC 2228 RFC 2228 RFC 959 RFC 2228 RFC 697 RFC 2228 RFC Abort an active file transfer Account information Authentication/Security data Allocate sufficient disk space to receive a file Append Authentication/Security mechanism Clear command channel Change to parent directory Confidentiality protection command Change working directory Delete file Privacy protected channel Specifies an extended address and port to which the server should connect Enter extended passive mode Get the feature list implemented by the server Help Language negotiation Description

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

4/7

29/04/2013
LIST RFC 1639 RFC 1639 RFC 3659 RFC 2228 RFC 959 RFC 3659 RFC 3659

File Transfer Protocol (FTP) Client - CodeProject


Returns information of a file or directory if specified, else information of the current working directory is returned Specifies a long address and port to which the server should connect Enter long passive mode Returns the last-modified time of a specified file Integrity protected command Make directory List the contents of a directory if a directory is named Provide data about exactly the object named on its command line, and no others Set the transfer mode (Stream, Block, or Compressed) Return a list of file names in a specified directory No operation (dummy packet; used mostly on keepalives) RFC 2389 Select options for a feature Authentication password Enter passive mode RFC 2228 Protection buffer size Specifies an address and port to which the server should connect RFC 2228 RFC 959 Data channel protection level Print working directory. Returns the current directory of the host. Disconnect Re-initialize the connection RFC 3659 Restart transfer from the specified point Transfer a copy of the file RFC 959 Remove a directory Rename from Rename to Send site specific commands to remote server RFC 3659 RFC 959 Return the size of a file Mount file structure Return the current status Accept the data and store the data as a file at the server site RFC 959 Store file uniquely Set file transfer structure RFC

LPRT LPSV MDTM MIC MKD MLSD MLST MODE NLST NOOP OPTS PASS PASV PBSZ PORT PROT PWD QUIT REIN REST RETR RMD RNFR RNTO SITE SIZE SMNT STAT STOR STOU STRU

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

5/7

29/04/2013
SYST TYPE USER XCUP XMKD XPWD XRCP XRMD XRSQ XSEM XSEN RFC 775 RFC 775 RFC 775 RFC 743 RFC 775 RFC 743 RFC 737 RFC 737 959

File Transfer Protocol (FTP) Client - CodeProject


Return system type Set the transfer mode (ASCII/Binary) Authentication username Change to the parent of the current working directory Make a directory Print the current working directory

Remove the directory

Send, mail if cannot Send to terminal

A short list of reply codes


2xx - Success reply 4xx or 5xx - Failure reply 1xx or 3xx - Error or Incomplete reply The second digit defines the kind of error: x0z x1z x2z x3z x4z x5z Syntax - These replies refer to syntax errors. Information - Replies to requests for information. Connections - Replies referring to the control and data connections. Authentication and accounting - Replies for the login process and accounting procedures. Not defined. File system - These replies relay status codes from the server file system.

By the way, the last FTP Technology is RCF 2428 since 1998. It's an unimportant information. Because it's about FTP's history. But we looked at the RCF 959 technology and it has been in use since 1985.

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

About the Author


SercanOzdemir
Turkey Member
Follow on Twitter Google

No Biography provided

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

6/7

29/04/2013
Article Top
Like 0

File Transfer Protocol (FTP) Client - CodeProject


0 Tw eet 1

Sign Up to vote Poor

Excellent

Vote

Comments and Discussions


Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'. You must Sign In to use this message board. Search this forum Profile popups Spacing Relaxed Noise Medium Layout Normal Per page 25 Go Update

First Prev Next

i can't open the zip file Re: i can't open the zip file not working Re: not working FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING Re: FTPClient no WORKING [modified] Advice Needed Re: Advice Needed My vote of 5 Re: My vote of 5 My vote of 5 Re: My vote of 5
Last Visit: 31 Dec '99 - 18:00 General News Last Update: 28 Apr '13 - 17:49 Suggestion Question Bug

Ibraheem Halloum SercanOzdemir kamleshgs SercanOzdemir campania SercanOzdemir SercanOzdemir campania SercanOzdemir campania SercanOzdemir campania SercanOzdemir Member 4242968 SercanOzdemir burak299 SercanOzdemir Sergio Andrs Gutirrez Rojas SercanOzdemir

4 Apr '13 - 23:16 4 Apr '13 - 23:21 14 Mar '12 - 5:09 15 Mar '12 - 3:18 8 Dec '11 - 6:26 8 Dec '11 - 6:50 8 Dec '11 - 10:14 9 Dec '11 - 1:38 9 Dec '11 - 2:37 9 Dec '11 - 2:58 9 Dec '11 - 3:42 9 Dec '11 - 3:06 9 Dec '11 - 3:45 5 Dec '11 - 5:23 5 Dec '11 - 11:05 2 Dec '11 - 3:39 2 Dec '11 - 4:41 1 Dec '11 - 16:26

2 Dec '11 - 0:50 Refresh 1

Answer

Joke

Rant

Admin
Article Copyright 2011 by SercanOzdemir Everything else Copyright CodeProject, 1999-2013 Terms of Use

Permalink | Advertise | Privacy | Mobile Web02 | 2.6.130426.1 | Last Updated 1 Dec 2011

Layout: fixed | fluid

www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client

7/7

Potrebbero piacerti anche