2016. 10. 25. 09:06 MFC/FTP
MFC) FTP 간단 예제 소스
[퍼옴] : http://xtelite.blogspot.kr/2007/04/mfc-ftp.html
CInternetSession m_Session;
CFtpConnection *m_pConnection;
CFtpFileFind *m_pFileFind;
생성자 함수에서 포인터 초기화
m_pFileFind = NULL;
m_pConnection = NULL;
Stdafx.h 제일 아래에 다음 추가(direct.h는 디렉토리 관련 함수 호출을 위한 것임.)
#include "afxsock.h"
#include "afxinet.h"
#include "direct.h"
InitInstance 함수에서 AfxSocketInit() 호출
CWinApp::InitInstance();
if (!AfxSocketInit()) {
AfxMessageBox(_T("소켓 초기화 실패"));
return FALSE;
}
...
이제 연결을 합니다.
BeginWaitCursor();
// 이미 연결된 것이 있으면 해제
if (m_pConnection != NULL) {
m_pConnection->Close();
delete m_pConnection;
m_pConnection = NULL;
}
...
// 연결하고자 하는 FPT 서버의 IP 주소와 사용자명, 암호 입력
m_pConnection = m_Session.GetFtpConnection(sIpAddress, sUsername, sPassword);
// 연결이 안됐을 경우
if (!m_pConnection) {
AfxMessageBox(_T("Error"));
m_pConnection = NULL;
return;
}
// 현재 디렉토리를 얻음
m_pConnection->GetCurrentDirectory(sRomoteDir);
// 이미 사용하고 있으면 해제
if (m_pFileFind) delete m_pFileFind;
// FTP 서버의 파일명을 읽어 옴
m_pFileFind = new CFtpFileFind(m_pConnection);
...
EndWaitCursor();
m_pFileFind는 다음과 같이 사용함.
먼저 디렉토리를 넘겨 주고 FileFind 한 번 호출, 그런 다음 FindNextFile 함수가 FALSE를 넘겨 줄 때까지 GetFileName 함수를 반복해서 호출한다.
m_pFileFind->FindFile(sRemoteDir);
...
while (bContinue) {
bContinue = m_pFileFind->FindNextFile();
sFileName = m_pFileFind->GetFileName();
...
}
m_pFileFind->IsDirectory()를 호출하면 디렉토리인지 폴더인지 확인 가능하다.
기타
m_pConnection->SetCurrentDirectory(sRemoteDir);
m_pConnection->GetCurrentDirectory(sRemoteDir);
등이 사용된다.
파일의 업로드/다운로드는
m_pConnection->PutFile(sLocalFile, sRemotePath);
m_pConnection->GetFile(sPath, sRemotePath)
'MFC > FTP' 카테고리의 다른 글
FTP) FTP란? (0) | 2016.12.19 |
---|---|
MFC) FTP 필수 클래스 - CFtpFileFind (2) | 2016.10.24 |
MFC) FTP 필수 클래스 - CFtpConnection (0) | 2016.10.24 |
MFC) FTP 필수 클래스 - CInternetSession (0) | 2016.10.24 |