网络协议FTP客户端MFC-VC程序

发布时间:2016-07-05 14:06:09

一.设计题目:

FTP客户端程序

二.设计要求:

程序能实现基本FTP客户端功能,能登陆FTP服务器,显示登陆客户目录下的文件和目录名,能从该目录中选择下载服务器的文件,也能向服务器上传文件。

1.该程序是基于对话框的MFC程序,程序界面布置要点:

1)三个文本编辑框,分别用于输入FTP服务器域名、登陆用户名和登陆口令。

2).一个列表框,用来显示FTP服务器当前目录的内容,并允许客户从中选择文件下载。

3).四个命令按钮分别执行查询、上传、下载和退出的功能。

三.源代码

//CrtDirDlg.h

#if !defined(AFX_CRTDIRDLG_H__2DE1DA5F_070C_4DEE_AAB8_759EF90C6B24__INCLUDED_)

#define AFX_CRTDIRDLG_H__2DE1DA5F_070C_4DEE_AAB8_759EF90C6B24__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// CrtDirDlg.h : header file

///////////////////////////////////////////////////////////////////////////////

// CCrtDirDlg dialog

class CCrtDirDlg : public CDialog

{

// Construction

public:

CString GetDirName();

CCrtDirDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(CCrtDirDlg)

enum { IDD = IDD_CRTDIR_DIALOG};

CString m_DirToCrt;

//}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CCrtDirDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(CCrtDirDlg)

// NOTE: the ClassWizard will add member functions here

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CRTDIRDLG_H__2DE1DA5F_070C_4DEE_AAB8_759EF90C6B24__INCLUDED_)

// MyFtp.h : main header file for the MYFTP application

//

#if !defined(AFX_MYFTP_H__EBAAEC27_5A1D_440C_82B1_385AE319A579__INCLUDED_)

#define AFX_MYFTP_H__EBAAEC27_5A1D_440C_82B1_385AE319A579__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__

#error include 'stdafx.h' before including this file for PCH

#endif

#include "resource.h" // main symbols

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp:

// See MyFtp.cpp for the implementation of this class

//

class CMyFtpApp : public CWinApp

{

public:

CMyFtpApp();

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CMyFtpApp)

public:

virtual BOOL InitInstance();

//}}AFX_VIRTUAL

// Implementation

//{{AFX_MSG(CMyFtpApp)

// NOTE - the ClassWizard will add and remove member functions here.

// DO NOT EDIT what you see in these blocks of generated code !

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYFTP_H__EBAAEC27_5A1D_440C_82B1_385AE319A579__INCLUDED_)

// MyFtpDlg.h : header file

//

#if !defined(AFX_MYFTPDLG_H__91E34BFF_2935_4334_BEE8_49C34942A679__INCLUDED_)

#define AFX_MYFTPDLG_H__91E34BFF_2935_4334_BEE8_49C34942A679__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#include < afxinet.h >

/////////////////////////////////////////////////////////////////////////////

// CMyFtpDlg dialog

class CMyFtpDlg : public CDialog

{

// Construction

public:

CMyFtpDlg(CWnd* pParent = NULL); // standard constructor

~CMyFtpDlg();

// Dialog Data

//{{AFX_DATA(CMyFtpDlg)

enum { IDD = IDD_MYFTP_DIALOG };

CListCtrl m_FileList;

CString m_HostEdit;

CString m_PasswordEdit;

CString m_UserEdit;

int m_port;

CString m_DirName;

//}}AFX_DATA

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CMyFtpDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

HWND CreateTipDlg();

void OnCurdir();

void OnChangdir();

CString GetTimeStr(FILETIME ftWrite);

void List();

int IndexInFieldList;//存储m_FileList的项目索引

int iItemCount;

CInternetSession *m_pInetSession;

CFtpConnection *m_pFtpConnection;

BOOL bConnect;

BOOL bFirstConnect;

CString strDirTmp;

HICON m_hIcon;

// Generated message map functions

//{{AFX_MSG(CMyFtpDlg)

virtual BOOL OnInitDialog();

afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

afx_msg void OnPaint();

afx_msg HCURSOR OnQueryDragIcon();

afx_msg void OnConnect();

afx_msg void OnSend();

afx_msg void OnDisconnect();

afx_msg void OnCreatedir();

afx_msg void OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult);

afx_msg void OnFatherdir();

afx_msg void OnDel();

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

private:

void Deletedir(CString strDirToDel);

void Deletefile(CString strFileToDel);

void DownLoadFile(CString strFileName);

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MYFTPDLG_H__91E34BFF_2935_4334_BEE8_49C34942A679__INCLUDED_)

//Resourse.h

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

// Used by MyFtp.rc

//

#define IDM_ABOUTBOX 0x0010

#define IDD_ABOUTBOX 100

#define IDS_ABOUTBOX 101

#define IDD_MYFTP_DIALOG 102

#define IDR_MAINFRAME 128

#define IDD_TIP_DIALOG 129

#define IDD_UPLOAD_DIALOG 130

#define IDD_CRTDIR_DIALOG 131

#define IDC_SEND 1000

#define IDC_IP 1001

#define IDC_USER 1002

#define IDC_PASSWORD 1003

#define IDC_FILENAME 1004

#define IDC_CONNECT 1005

#define IDC_DISCONNECT 1006

#define IDC_GETFILE 1007

#define IDC_CREATEDIR 1008

#define IDC_DELETEFILE 1009

#define IDC_DELETEDIR 1010

#define IDC_RENAME 1011

#define IDC_CURDIR 1012

#define IDC_DIRNAME 1013

#define IDC_CHANGDIR 1014

#define IDC_PORT 1015

#define IDC_LIST 1025

#define IDC_FATHERDIR 1026

#define IDC_TIPSTR 1027

#define IDC_DEL 1028

#define IDC_LOCALNAME 1029

#define IDC_REMONAME 1030

#define IDC_EDIT1 1031

// Next default values for new objects

// #ifdef APSTUDIO_INVOKED

#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE 132

#define _APS_NEXT_COMMAND_VALUE 32771

#define _APS_NEXT_CONTROL_VALUE 1032

#define _APS_NEXT_SYMED_VALUE 101

#endif

#endif

// stdafx.h : include file for standard system include files,

// or project specific include files that are used frequently, but

// are changed infrequently/

#if !defined(AFX_STDAFX_H__0475838D_1965_4B07_B23F_E5F09F60B164__INCLUDED_)

#define AFX_STDAFX_H__0475838D_1965_4B07_B23F_E5F09F60B164__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers

#include // MFC core and standard components

#include // MFC extensions

#include // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT

#include // MFC support for Windows Common Controls

#endif // _AFX_NO_AFXCMN_SUPPORT

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__0475838D_1965_4B07_B23F_E5F09F60B164__INCLUDED_)

//TipDlg.h

#if !defined(AFX_TIPDLG_H__41571539_B4FD_4834_B77D_159E8601520F__INCLUDED_)

#define AFX_TIPDLG_H__41571539_B4FD_4834_B77D_159E8601520F__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// TipDlg.h : header file

//

#define WM_MY_EXIT WM_USER+100

/////////////////////////////////////////////////////////////////////////////

// CTipDlg dialog

class CTipDlg : public CDialog

{

// Construction

public:

CTipDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(CTipDlg)

enum { IDD = IDD_TIP_DIALOG };

// NOTE: the ClassWizard will add data members here

//}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CTipDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(CTipDlg)

afx_msg LRESULT OnMyExit(WPARAM wParam, LPARAM lParam);

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_TIPDLG_H__41571539_B4FD_4834_B77D_159E8601520F__INCLUDED_)

//UpDlg.h

#if !defined(AFX_UPDLG_H__E5B966C0_730D_4659_8400_7AC356BD55A3__INCLUDED_)

#define AFX_UPDLG_H__E5B966C0_730D_4659_8400_7AC356BD55A3__INCLUDED_

#if _MSC_VER > 1000

#pragma once

#endif // _MSC_VER > 1000

// UpDlg.h : header file

///////////////////////////////////////////////////////////////////////////////

// CUpDlg dialog

class CUpDlg : public CDialog

{

// Construction

public:

CString GetDstFile();

CString GetSrcFile();

CUpDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data

//{{AFX_DATA(CUpDlg)

enum { IDD = IDD_UPLOAD_DIALOG };

CString m_SrcFile;

CString m_DstFile;

//}}AFX_DATA

// Overrides

// ClassWizard generated virtual function overrides

//{{AFX_VIRTUAL(CUpDlg)

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

//}}AFX_VIRTUAL

// Implementation

protected:

// Generated message map functions

//{{AFX_MSG(CUpDlg)

// NOTE: the ClassWizard will add member functions here

//}}AFX_MSG

DECLARE_MESSAGE_MAP()

};

//{{AFX_INSERT_LOCATION}}

// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_UPDLG_H__E5B966C0_730D_4659_8400_7AC356BD55A3__INCLUDED_)

// CrtDirDlg.cpp : implementation file

//#include "stdafx.h"

#include "MyFtp.h"

#include "CrtDirDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CCrtDirDlg dialog 创建目录

CCrtDirDlg::CCrtDirDlg(CWnd* pParent /*=NULL*/)

: CDialog(CCrtDirDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CCrtDirDlg)

m_DirToCrt = _T("");

//}}AFX_DATA_INIT

}

void CCrtDirDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CCrtDirDlg)

DDX_Text(pDX, IDC_EDIT1, m_DirToCrt);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CCrtDirDlg, CDialog)

//{{AFX_MSG_MAP(CCrtDirDlg)

// NOTE: the ClassWizard will add message map macros here

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CCrtDirDlg message handlers

CString CCrtDirDlg::GetDirName() //输入创建目录的名称

{

return m_DirToCrt;

}

// MyFtp.cpp : Defines the class behaviors for the application.

#include "stdafx.h"

#include "MyFtp.h"

#include "MyFtpDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp

BEGIN_MESSAGE_MAP(CMyFtpApp, CWinApp)

//{{AFX_MSG_MAP(CMyFtpApp)

// NOTE - the ClassWizard will add and remove mapping macros here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG

ON_COMMAND(ID_HELP, CWinApp::OnHelp)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp construction

CMyFtpApp::CMyFtpApp()

{

// TODO: add construction code here,

// Place all significant initialization in InitInstance

}

/////////////////////////////////////////////////////////////////////////////

// The one and only CMyFtpApp object

CMyFtpApp theApp;

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp initialization

BOOL CMyFtpApp::InitInstance()

{

// Standard initialization

// If you are not using these features and wish to reduce the size

// of your final executable, you should remove from the following

// the specific initialization routines you do not need.

#ifdef _AFXDLL

Enable3dControls(); // Call this when using MFC in a shared DLL

#else

Enable3dControlsStatic(); // Call this when linking to MFC statically

#endif

CMyFtpDlg dlg;

m_pMainWnd = &dlg;

int nResponse = dlg.DoModal();

if (nResponse == IDOK)

{

// TODO: Place code here to handle when the dialog is

// dismissed with OK

}

else if (nResponse == IDCANCEL)

{

// TODO: Place code here to handle when the dialog is

// dismissed with Cancel

}

// Since the dialog has been closed, return FALSE so that we exit the

// application, rather than start the application's message pump.

return FALSE;

}

// MyFtp.cpp : Defines the class behaviors for the application.

//#include "stdafx.h"

#include "MyFtp.h"

#include "MyFtpDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp

BEGIN_MESSAGE_MAP(CMyFtpApp, CWinApp)

//{{AFX_MSG_MAP(CMyFtpApp)

// NOTE - the ClassWizard will add and remove mapping macros here.

// DO NOT EDIT what you see in these blocks of generated code!

//}}AFX_MSG

ON_COMMAND(ID_HELP, CWinApp::OnHelp)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp construction

CMyFtpApp::CMyFtpApp()

{

// TODO: add construction code here,

// Place all significant initialization in InitInstance

}

////////////////////////////////////////////////////////////////////////////

// The one and only CMyFtpApp object

CMyFtpApp theApp;

/////////////////////////////////////////////////////////////////////////////

// CMyFtpApp initialization

BOOL CMyFtpApp::InitInstance()

{

// Standard initialization

// If you are not using these features and wish to reduce the size

// of your final executable, you should remove from the following

// the specific initialization routines you do not need.

#ifdef _AFXDLL

Enable3dControls(); // Call this when using MFC in a shared DLL

#else

Enable3dControlsStatic(); // Call this when linking to MFC statically

#endif

CMyFtpDlg dlg;

m_pMainWnd = &dlg;

int nResponse = dlg.DoModal();

if (nResponse == IDOK)

{

// TODO: Place code here to handle when the dialog is

// dismissed with OK

}

else if (nResponse == IDCANCEL)

{

// TODO: Place code here to handle when the dialog is

// dismissed with Cancel

}

// Since the dialog has been closed, return FALSE so that we exit the

// application, rather than start the application's message pump.

return FALSE;

}

// stdafx.cpp : source file that includes just the standard includes

// MyFtp.pch will be the pre-compiled header

// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TipDlg.cpp : implementation file

//

#include "stdafx.h"

#include "MyFtp.h"

#include "TipDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CTipDlg dialog 下载文件

CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/)

: CDialog(CTipDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CTipDlg)

// NOTE: the ClassWizard will add member initialization here

//}}AFX_DATA_INIT

}

void CTipDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CTipDlg)

// NOTE: the ClassWizard will add DDX and DDV calls here

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CTipDlg, CDialog)

//{{AFX_MSG_MAP(CTipDlg)

ON_MESSAGE(WM_MY_EXIT, OnMyExit)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CTipDlg message handlers

LRESULT CTipDlg::OnMyExit(WPARAM wParam,LPARAM lParam)

{

if (wParam==0) SetDlgItemText(IDC_TIPSTR,"下载完成.");

else SetDlgItemText(IDC_TIPSTR,"下载失败.");

Sleep(500);

CDialog::OnOK();

return 0;

}

// UpDlg.cpp : implementation file

//

#include "stdafx.h"

#include "MyFtp.h"

#include "UpDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

// CUpDlg dialog 上传文件

CUpDlg::CUpDlg(CWnd* pParent /*=NULL*/)

: CDialog(CUpDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CUpDlg)

m_SrcFile = _T("");

m_DstFile = _T("");

//}}AFX_DATA_INIT

}

void CUpDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CUpDlg)

DDX_Text(pDX, IDC_LOCALNAME, m_SrcFile);

DDX_Text(pDX, IDC_REMONAME, m_DstFile);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CUpDlg, CDialog)

//{{AFX_MSG_MAP(CUpDlg)

// NOTE: the ClassWizard will add message map macros here

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CUpDlg message handlers

CString CUpDlg::GetSrcFile()

{

return m_SrcFile;

}

CString CUpDlg::GetDstFile()

{

return m_DstFile;

}

网络协议FTP客户端MFC-VC程序

相关推荐