Implement FTP function in the VC

xiaoxiao2021-03-06  69

Article Title: Implementation of FTP function in VC: Meng Xuewang 出: LOOSE_WENT Release Type: Reprinted Release Date: 2004-04-12 Today Views: 6 Total Views: 1523

---- To join the FTP server, you need two steps, you must first create a CinterNetSession object, create and initialize one or several simultaneous Internet sessions, and describe the connection with the proxy server (if If necessary, if you need to keep your connection with the Internet during the program, you can create a CinternetSession object as a member of CWINAPP. ---- Class CFTPConnection in the MFC manages our connection to the Internet server, and manipulate directory and files on the server, FTP is one of the three Internet features of the MFC Wininet support, we need to create a CinternetSession instance and one CFTPConnection objects can implement communication with an FTP server, we don't need to create a CFTPConnection object directly, but complete this work by calling CinternetSession :: GetFTPConnection. It creates a CFTPCONNECTION object and returns a pointer to this object. ---- The following information Ftp connection classes we briefly describe the information CInternetSession connected objects class ---- CInternetSession (LPCTSTR pstrAgent, DWORD dwConText, DWORD dwAccessType, LPCTSTR pstrProxyName, LPCTSTR pstrProxyBypass, DWORD dwFlags); ---- in creation Call this member function when the CinternetSession object, CinternetSession is the first Internet function to be called, which will create internal data structures for future calls in applications. If dwflags contains Internet_flag_async, all handles are derived from this handle, there is an asynchronous state before the status callback routine is registered. If you don't open the Internet connection, CinternetSession will throw an exception, AfXThorowInternetException. GetFtpConnection () function ---- CFtpConnection * CIternetSession :: GetFtpConnection (LPCTSTR pstrServer, LPCTSTR pstrUserName, LPCTSTR pstrPassword, INTERNET_PORT nPort, BOOL bPassive); ---- call this function to establish a FTP connection, and get a pointer to the object CFtpConnection The pointer, GetFTPConnection is connected to an FTP server, creates and returns a pointer to the CFTPConnection object, which is not in the server. If you plan to read and write files, you must step by step. For information on finding, opening and reading / writing files, refer to the CFTPConnection and CFTPFILEFIND classes. ---- Returns a pointer to the CFTPConnection object to the call to this function. If the call failed, check the thrown CinternetException object, you can determine the reason for the failure. GetFile () Function ---- Bool getFile (LPCTSTSTSTRLOCALFILE, LPCTSTSTR PSTRLOCALFILE, BOOL BFAILEXISTS, DWORD DWATTRIBUTES, DWORD DWFLAGS, DWORD DWCONTEXT); ---- Calling this member function, you can get files from the FTP server, and save files On the local machine.

The getFile () function is a relatively advanced routine that handles all the works from the FTP server read files and stores files on the local machine. If DWFLAGS is file_transfer_type_ascii, the transfer of file data will also convert control and formats into the equity symbols in Windows. The default transmission mode is binary mode, files are downloaded in the same format on the server. ---- PSTRREMOTEFILE and PSTRLOCALFILE can be part of the file names relative to the current directory, or all file names, in the middle of these two names, they can be used as a backslash (/) or a front slash (/) as a Directory separators of the file name, getFile () transforms directory separators to the appropriate characters before use. ---- You can use your own value to replace DWCONTEXT default values, setting to the context identifier is related to the positioning operation of the CFTPConnection object, which is created by the CFTPConnection in the CinternetSession object. The value returned to CinternetSession :: OnStatusCallback indicates the status of the identified operation. ---- If the call is successful, the return is not 0, otherwise it returns 0. If the call fails, you can call the Win32 function getLastError () to confirm the reason for the error. Putfile () function ---- Bool Putfile (LPCTSTSTR PSTRLOCALFILE, DWORD DWFLAGS, DWORD DWCONTEXT); ---- calling this member function to save the file to the FTP server. The Putfile () function is a relatively advanced routine that can handle the work on putting the file on the server. Only send data, or to strictly control the application for file transfer, you should call OpenFile and Cinternet :: Write. Using the value of your own choice to replace the DWContext default value, set to the context identifier, the context identifier is the specific operation of the CFTPConnection object created by the CinternetSession object, this value is returned to CinternetSession :: OnStateCallback, thus report the status of the operation to it The context identified. ---- If the call is successful, the return is not 0, otherwise it returns 0. If the call fails, you can call the Win32 function getLastError () to confirm the reason for the error. Connect to the FTP site ---- Establish a program that connects to ftp.microsoft.com, which is a single document program. And the connection is done by the constructor of the view class.

Establish a single document program FTP to add incorporated #include to ftpView.h Add the following member variables PUBLIC: CinternetSession * m_pinetSession; cftpconnection * m_pftpConnection; add in ftpView constructor in ftpView.cpp the following code CFtpView :: CFtpView () {m_pInetSession = new CInternetSession (AfxGetAppName (), 1, PRE_CONFIG_INTERNET_ACCESS); try {m_pFtpConnection = m_pInetSession-> GetFtpConnection ( "FTP.MICROSOFT.COM");} catch (CInternetException * pEx) { TCHAR SZERROR [1024]; IF (PEX-> GetErrorMessage (Szerror, 1024)) AFXMessageBox (Szerror); Else AfxMessageBox; PEX-> delete (); m_pftpconnection = null;}} in ftpView. Add the following code cftpview :: ~ cftpView () {if (m_pftpconnection! = null) {m_pftpconnection-> close (); delete m_pstption-> close (); DELETE M_PFTPCONNECTION;} DELETE M_PINETSESSION; There will be a problem in a message box.

转载请注明原文地址:https://www.9cbs.com/read-108386.html

New Post(0)