TJFTP - A Visual C++ FTP Client

A Windows FTP client written without CInternetSession or CFtpConnection classes. Demonstrates manual manipulation of Winsock sockets, FTP principles, and GUI concepts such as List controls with in-place label-editing and column sorting, progress indicators, and reading and writing to the Registry.

Introduction

Ever wondered what goes on behind the scenes when you use an FTP client? What actually happens at the socket level, and what the program does in the background?

TJFTP is a CFormView-derived FTP (File Transfer Protocol) application written in Visual C++ 4 and 5. VC++ had no CInternetSession, CFtpConnection, CAsyncSocket, CSocket, or Socket MFC classes back then, so I wrote in the FTP functionality from scratch, creating custom socket classes to handle the control and data connections. Then I coded the GUI to manipulate directory and file creation, deletion, and transfer once I had the connection, and to stay updated with the progress of operations.

Background

Back in 1996, I was writing some apps for a computer engineering master's final project. I decided to see if I could write a fully functional FTP app that would incorporate some nice GUI stuff. Features I wanted included; a Connection dialog box to allow users to create and save connection profiles in the Registry; List controls with in-place label editing and column sorting on both sides of the connection; double-clicking to initiate file transfers; selecting and transferring multiple files; and a progress indicator to show the user the status of file transfers. Having never programmed before, I bought VS 4 and, after mastering the "Hello World" beginner's app we all write, got to work.

Early in 2005, I stumbled on the source code to TJFTP, which I thought I'd lost. I considered recompiling it in VC++.NET for fun, but I was sure it would blow up, after 8 years and several iterations of Visual C++, Windows, and WinSock. However, it compiled under .NET with no errors, and ran fine on Windows XP.

Some of the Custom Classes

CStreamSocket (csock.cpp): Creates and destroys control and data sockets. Creates and accepts connections, and sends and receives data. Handles Winsock events, including asynchronous read and write events to prevent blocking (i.e. freezing until the Winsock calls were done).

CFtpConnect (ftpconnect.cpp): A CDialog-based class that opens a dialog box for the user to input connection info into when the the app starts and when the "Connect" button is pushed. Stores connection "profiles" in the Registry so users save their information from session-to-session.

CMainView (mainview.cpp): The real "guts" of the program. Handles all the GUI stuff for the main interface, including list controls, pushbuttons, and the progress indicator. Processes Winsock messages, creates sockets from CStreamSocket, listens to connections, and accepts connections and data. Allows user to select binary or ASCII transfer, or lets the program auto detect the transfer mode.

Points of Interest

Documentation: Because I was writing this app as part of a master's thesis, the project had to be documented to software engineering standards. Each subroutine has a complete header comment, and there are comment lines sprinkled liberally throughout the code explaining what's going on. In addition, I wrote a user's manual, and I've also extracted the portion of my thesis that deals with TJFTP. It has detailed information on each subroutine, and program flow diagrams, such as the main flow diagram below (dotted lines are asynchronous Winsock callbacks):

TJFTP Main Program Flow Diagram

The flow diagrams show how convoluted the whole process is, and the difficulty in keeping track of everything going on. Code snippets aren't really appropriate for this article, because there is so much going on. I suggest you grab the flow diagrams, open the code in VS, and dig around.

Functionality: I've tested the recompiled program on WinXP SP1 and SP2, uploading files to my FTP server, then downloading them back to my local machine with both TJFTP and a commercial FTP client. Then I checked the file to see if it matched the original file. The largest file I tested was 300 MB. As you can see from the screenshot above, I also logged on to a commercial server that allows anonymous connections and successfully downloaded files. TJFTP seems very stable, it hasn't crashed on me yet.

Caveat

This program is provided for informational use only. Although I have used it without file corruption or other problems, I make no claims to this effect...do not use it as you would a commercial FTP app. Also, TJFTP is not secure...passwords are sent in plain text and are not encrypted.

-- which is running on .NET Framework 1.1 or higher,
-- which can automatically put together splitted files on the server,
-- which allows to download only a part of a file on the server,
-- which allows to resume any broken download,
-- which automatically starts a separate thread,
-- which can be aborted any time from your main thread,
-- which supports UTF8 encoded filenames,
-- which has a built-in download scheduler,
-- which has a built-in bandwidth control,
-- which has a built-in preview function for the download of movies,
-- which automatically reconnects the server after an error has occurred,
-- which displays download progress in percent and in bytes and the remaining time,
-- which writes a detailed logging for all operations it does,
-- which is based on Wininet.dll and has one workaround for each of the 4 known Wininet.dll bugs,
-- which is very well tested and bug-free,
-- which is written by a very experienced programmer and has a very clean and well documented sourcecode,

I would like to introduce myself first, Omar Ali, MSc student in the research stage, college of Engineering, Baghdad University, Iraq.
I am making research on (Active Networking System- Security of Active Networks).I found out throughout my research that I need FTP client under windows to help me accomplishing my research.

I request your kind assistance to help providing me with answer to the following question about "TJFTP" software.

What is the problem please? I need correct building of the TJFTP code. I would like to make some modifications to the original TJFTP software (security, for example). I will, surely, refer to the original TJFTP author and site in references of my thesis. "

Knowing that I have been visited your site and find it related (in some cases) to my research.

Your kind assistance in providing me with answers to this question will help me allot to tackle and overcome the problems I am facing in my research.

Hi! This is a really interesting piece of work and I want to be able to understand the program flow by stepping through in debug mode - I'm a novice. The program compiles and runs fine in release mode. I noted your advice to another correspondent that it must be run in release. The reason for not using debug mode, presumably, is the error that occurs in mainview.cpp in

OnStream(WPARAM wParam, LPARAM lParam)

line 695 CString csTemp,
line 696 csDoneWrite;

This throws an error for no reason that I can see. Do you have a fix for this please? I'm using Visual C++ .NET.

1. Open Resource View, expand "Dialog", and double-click on IDD_TJFTP_FORM.

2. Drag a button from the Toolbox and drop it on the dialog. Name it IDC_BUTTON_ABOUT, give it the Text "About TJFTP".

Normally, you could now right-click the button and select "Add Event Handler" to have VS write the stub-code for the button in whatever class you want. However, VS 03 apparently can't do this correctly with older, converted projects, so we have to do it manually (which is also a good exercise to understand what is going on ). We'll put the button handlers in the CMainView class.

3. In mainview.cpp, add message handler, OnClickedButtonAbout, at the end of the message map:
...
ON_BN_CLICKED(IDC_BUTTON_MAIN_EXIT, OnButtonMainExit)
// This is the new line you add
ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnClickedButtonAbout)
//}}AFX_MSG_MAP

I just rechecked the source code download with VC++ 03, and it compiles fine. Are you compiling in "Release" or "Debug" mode? Should be "Release".

The reusability point is silly. As I said in the article, this project is for informational use. It was not designed to be modular, or an MFC class. If you want that, use the built in ftp classes in VS. And because FTP is event-driven, the GUI and the implementation go hand-in-hand.

As I stated in the article, the app is CFormView-derived, which you should know means that the GUI is based on a dialog, and therefore not resizable. Resizability was not a concern when I did this project. Creating a resizable app is a trivial matter, but if you need help, let me know.

As for "and also variables are to initiliaze, and to check before use", I have no idea what this means, or how it has any bearing on the intent of the article, which is to demonstrate basic FTP programming priciples.

This is an 8-year old app that was the first program I ever wrote (and helped get me an A+ on my Master's Thesis ). I have no plans (or time) for "improvement".