1. Use MFC to write FTP Program
1. Generate the cinternetsession variable.
Cinternetsession ses;
2. Open the FTP connection.
Cftpconnection * pftp = SES. getftpconnection ("ftp.microsoft.com", "user", "password", port, false );
3. open the file.
Cinternetfile * pF = pftp-> openfile ("abc.exe ");
It is simpler to use
Cftpconnect: GetFile (...); cftpconnection: putfile (...); you can copy files.
4. Read files. Since cinternetfile is the derived class of cstdfile, you can use all functions of cfile and cstdfile.
PF-> Read (...)
Advanced usage:
1. Find the file and column file directory. (FTP tree sample in vc5)
The cftpfilefind class can help you list all files in the current directory. The usage is the same as that of cfilefind.
2. resumable upload.
After opening the file, you can use cinternetfile: Seek (...) to relocate the pointer. However, if the server does not support resumable data transfer, an exception may occur and a corresponding capture is required.Code.
Others:
The usage of the same chttpconnection is similar.
2. Hanging up the line after using the Internet
Wininet APIs provide powerful functions, but when Internet transactions are closed, the dial up connection cannot be closed. The corresponding function is not provided in wininet API to complete this function. So how can we close the dial up connection while disabling Internet transactions?
Solution:
The ras api can close the connection by calling rashangup (...), but the RAS handle is required. There are two ways to handle Ras properly:
1. If you create a RAS connection by yourself, it is unnecessary to save the handle,
2. Use rasenumconnections (...) to list all handles.
Required environment:
Rasapi32.dll. The Connection Library is automatically installed when the network of the multicast number is installed.
RAS. h
Rasapi32.lib
DEMO code:
// Quit a dial up connection-rasapi32.lib und # include "RAS. H" needed
Bool disconnectras ()
{
Bool Bok = false;
Rasconn Ras [20];
DWORD dsize, dnumber, dcount;
Ras [0]. dwsize = sizeof (rasconn );
Dsize = sizeof (RAS );
// Get active Ras-connection
If (rasenumconnections (Ras, & dsize, & dnumber) = 0)
{
Bok = true;
for (dcount = 0; dcount {< br> // hang up that connection
If (rashangup (RAS [dcount]. hrasconn )! = 0)
{< br> Bok = false;
break;
}< BR >}< br> return Bok;
}