2007년 4월 3일 화요일

CFtpConnection 예제 정리

void ParseCmdLine(int argc, TCHAR* argv[], TCHAR* envp[]);
void Say(CString status);

int TMainApp(int argc, TCHAR* argv[], TCHAR* envp[]) {

    Say(L"Parsing command line");
    ParseCmdLine(argc, argv, envp);

    Say(L"Allocating variables");
    CString sIpAddress = L"127.0.0.1";
    CString sUsername = L"username";
    CString sPassword = L"password";
    CString sRemoteDir;
    CString sLocalDir;
    CString sFilename;

    CInternetSession m_Session;
    CFtpConnection *m_pConnection = NULL;
    CFtpFileFind *m_pFileFind = NULL;

    Say(L"Initializing...");
    if (!AfxSocketInit()) {
        throw CString(L"Failed socket initialization");
    }

    Say(L"Checking m_pConnection");
    // If in use, delete it
    if (m_pConnection != NULL) {
        m_pConnection->Close();
        delete m_pConnection;
        m_pConnection = NULL;
    }

    Say(L"m_Session.GetFtpConnection()");
    m_pConnection = m_Session.GetFtpConnection(sIpAddress, sUsername, sPassword);
    // If connection failed
    if (!m_pConnection) {
        m_pConnection = NULL;
        throw CString(L"m_Session.GetFtpConnection() failed");
    }

    Say(L"m_pConnection->GetCurrentDirectory()");
    // Get current remote directory
    m_pConnection->GetCurrentDirectory(sRemoteDir);
    // If Filefind is in use delete it
    if ( m_pFileFind ) delete m_pFileFind;
    // Get file name from the FTP server
    m_pFileFind = new CFtpFileFind(m_pConnection);

    Say(L"\r\n\r\nRetreiving file names...");
    BOOL bContinue = TRUE;
    m_pFileFind->FindFile(sRemoteDir);
    while ( bContinue ) {
        bContinue = m_pFileFind->FindNextFile();
        sFilename = m_pFileFind->GetFileName();
        if ( m_pFileFind->IsDirectory() ) {
            sFilename.Insert(0, '[');
            sFilename.Insert(sFilename.GetLength(), ']');
        }
        wcout << (LPCTSTR)sFilename << endl;
    }

    Say(L"\r\nProgram exit (Normal state)");
    return 0;
}

void ParseCmdLine(int argc, TCHAR* argv[], TCHAR* envp[]) {

//  if ( argc != 2 )
//      throw CString(L"Usage : inet.exe ip_address");

}

void Say(CString status) {
    wcout << (LPCTSTR)status << endl;
}


댓글 없음: