Escolar Documentos
Profissional Documentos
Cultura Documentos
home
articles
quick answers
discussions
features
community
help
Next
Article Browse Code Stats Revisions (7) Alternatives Comments & Discussions (19)
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
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
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."
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
www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client
2/7
29/04/2013
It'll look like this. Now we need a treeview. A few buttons. And a search feature.
Collapse | Copy Code
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
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
{
S t r i n g
} 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 :)
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
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
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)
No Biography provided
www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client
6/7
29/04/2013
Article Top
Like 0
Excellent
Vote
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
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
www.codeproject.com/Articles/293391/File-Transfer-Protocol-FTP-Client
7/7