Use VC ++ 5.0 to write FTP client programs

Source: Internet
Author: User
Tags ftp client ftp protocol

 

Use VC ++ 5.0 to write FTP client programs

With the rapid development of Internet, the development and design of network software become more and more important. The original network software mainly used UNIX operating systems as the software development environment. With the popularity of Windows personal operating systems, the transformation of traditional programming interfaces to this new software and hardware platform became extremely urgent. The C ++ 5.0 version of MFC encapsulates the csocket class to provide advanced socket support, which provides great convenience for compiling C/S programs based on windows in the Internet environment. This article describes how to use the csocket class to compile an FTP client program and uncover the secrets of network programming.

Winsock provides a powerful function set as a dynamic link library for programmers. Through calling this function set, applications can complete their specific tasks. However, the disadvantage is that the program is cumbersome. To solve this problem, Microsoft gradually improved its Visual C ++ series basic class library (MFC. In particular, the recently released version of VC ++ 5.0 encapsulates many classes related to network programming. Csocket is one of them.

The csocket class (the parent class is casyncsocket) provides an advanced socket support for low-level function operations, greatly reducing programming difficulty. Here, we use Windows 95 as the development environment and Visual C ++ 5.0 to compile an FTP client program to illustrate how to use the csocket class to develop network software in depth and effectively. Considering that an FTP server should be created in C/S mode, select 4.00.950b for Windows 95 because this version contains personal web servers and provides HTTP and FTP services.

First, establish a basic framework for SDI (Single Document Interface) applications. This step is relatively simple. In VC ++ 5.0, MFC Appwizard is activated by creating a new project. Select the new option in the File menu and select the project, enter the file name as superftp and select OK. The subsequent steps are the VC ++ automatic creation process. For more information, see. Finally, the following main classes are generated:
Cmainframe,
Csuperftpapp,
Csuperftpdoc,
Csuperftpview,
Caboutdlg.
Next, create several new classes, as shown in the following table:
For more information about the FTP protocol, see this document. This is an important prerequisite for the correct development of FTP client programs.
Step 3: Compile the specific program. Because the entire program is long, the core code of the main part is provided below with comments.
1. mainfrm. cpp:
......
Cmainframe: cmainframe ()
{
// Initialize the pointer
M_ctrlconn = NULL;
M_dataconn = NULL;
M_recvconn = NULL;
}
// Select the menu item "quick connection"
Void cmainframe: onquickconnect ()
{
If (! Makeconn ())
MessageBox ("FTP control link setup failed !", "Prompt", mb_iconwarning );
If (! Makeremotedir ())
MessageBox ("An error occurred while establishing the FTP data link !", "Prompt", mb_iconwarning );
}
// Establish a control link
Bool cmainframe: makeconn ()
{
......
Quickconn DLG;
// Enter the server name, user name, and password
If (DLG. domodal () = idok)
{
Fservername = DLG. m_servername;
Fusername = DLG. m_username;
Fpassword = DLG. m_password;
}
M_ctrlconn = new ctrlsocket ();
// Create a socket
If (! M_ctrlconn-> Create (0, sock_stream, null)
{
Delete m_ctrlconn;
M_ctrlconn = NULL;
MessageBox ("socket () creation failed !", "Prompt", mb_iconwarning );
Return false;
}
// Apply for network Event Notifications
If (! M_ctrlconn-> asyncselect (fd_read | fd_write | fd_accept | fd_connect | fd_close ))
{
MessageBox ("asyncselect () Error !", "Prompt", mb_icnwarning );
Return false;
}
Beginwaitcursor ();
// Send a connection request to the host specified by fservername
If (! M_ctrlconn-> connect (fservername, ipport_ftp ))
{
Delete m_ctrlconn;
M_ctrlconn = NULL;
MessageBox (remote server connection failed !", "Prompt", mb_iconwarning );
Return false;
}
Endwaitcursor ();
......
Return true;
}
Here, after selecting the "quick connection" item in the menu, the execution result is as follows:
If you select "anonymous" login, the application automatically fills in the user name and password words as: Anonymous, guest @ unknown
Bool cmainframe: makeremotedir ()
{
If (m_ctrlconn = NULL)
{
("Please connect to the server !", "Prompt", mb_iconwarwing );
Retrurn false;
}
If (! Makedatalisten ())
{
MessageBox ("Data Link creation failed !", "Prompt", mb_iconwarning );
Return false;
}
......
Return true;
}
// Process the response from the remote server
Bool cmainframe: processftp ()
{
.........
}
// Create a data connection listener Function
Bool cmainframe: makedatalisten ()
{
......
M_dataconn = new listensocket ();
/* Create a socket and bind it with the local host address. As an example, the local IP Address "90.0.0.8" is directly inserted. In practice, the local host address should be obtained through the corresponding function call.
Note: if other methods are used instead of the VC ++ csocket class, you need to call the BIND () function after creating the socket to bind the local host address. */
If (! M_dataconn-> Create (0, sock_stream, "90.0.0.8 "))
{
Delete m_dataconn;
M_dataconn = NULL; MessageBox ("socket () failed to be created !", "Prompt", mb_iconwarning );
Return false;
}
// Apply for network Event Notifications
If (! M_dataconn-> asyncselect (fd_accept ))
{
Delete m_dataconn;
M_dataconn = NULL;
MessageBox ("asyncselect () Error !", "Prompt", mb_iconwarning );
Return false;
}
......
If (! M_dataconn-> listen (5 ))
{
MessageBox ("Listen () Error !", "Prompt", mb_iconwarning );
Return false;
}
......
}
// Functions that receive data connection requests
Bool cmainframe: acceptdataconn ()
{
Int num, nret;
Sockaddr_in remotedataaddr;
Num = sizeof (sockaddr_in );
If (m_recvconn = NULL)
{
M_recvconn = new datasocket ();
}
If (! M_dataconn-> Accept (* m_recvconn, (lpsockaddr) & remotedataaddr, (INT far *) & num ))
{
MessageBox ("accept () Error !", "Prompt", mb_iconwarwing );
Return false;
}
......
M_dataconn-> close ();
// Apply for network Event Notifications
If (! M_recvconn-> asyncselect (fd_read | fd_write | fd_close)
{
MessageBox ("asyncselect () Error !", "Prompt", mb_iconwarning );
Return false;
}
Return true;
}
// Data receiving function, which accepts data from the remote server
Int cmainframe: recvdata ()
{
......
Int nret = m_recvconn-> receive (...);
If (nret> 0)
{
...
}
Else
{
...
}
......
}
2. ctrlsocket. cpp
......
Void ctrlsocket: onreceive (INT nerrorcode)
{
// Process network event fd_read
Csocket: onreceive (nerrorcode );
(Cmainframe *) (afxgetapp ()-> m_pmainwnd)-> processftp ();
}
3. listensocket. cpp
......
Void listensocket: onaccept (INT nerrorcode)
{
// Process network event fd_accept
Csocket: onaccept (nerrorcode );
(Cmainframe *) afxgetapp ()-> m_pmainwnd)-> acceptdataconn ();
}
4. datasocket. cpp
......
Datasocket: onreceive (INT nerrorcode)
{
// Process network event fd_read
Casyncsocket: onreceive (nerrorcode );
(Cmainframe *) (afxgetapp ()-> m_pmainwnd)-> recvdata ();
}
Void datasocket: onsend (INT nerrorcode)
{
// Process network event fd_write
Casyncsocket: onsend (nerrorcode );
(Cmainframe *) (afxgetapp ()-> m_pmainwnd)-> senddata ();
}

When the program is executed, the lower-left window is the local system, and the lower-right window is the remote system. By activating related menu items, you can download the file system of the remote host. Of course, if the FTP server allows upload, you can also transmit your own files to the remote host.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.