网络编程实验ftp.docx
- 文档编号:5501587
- 上传时间:2022-12-17
- 格式:DOCX
- 页数:13
- 大小:61.69KB
网络编程实验ftp.docx
《网络编程实验ftp.docx》由会员分享,可在线阅读,更多相关《网络编程实验ftp.docx(13页珍藏版)》请在冰豆网上搜索。
网络编程实验ftp
实验报告
课程网络编程实验名称CSocket类编程第页
专业___计算机_______班级__2班___学号_105032010148_姓名谢剑飞
实验日期:
2013年5月14日报告退发(订正、重做)
一、实验目的
☐熟悉MFCWinInet类的成员函数及其使用
☐掌握使用WinInet类编写客户端程序的一般流程和方法
☐能够使用WinInet类编写客户端应用程序
二、实验内容
使用WinInet类设计一个FTP客户端程序,具有FTP客户端应用程序的一般功能。
能够访问FTP服务器,并对FTP服务器上的资源进行操作。
三、实验环境
操作系统:
Windows操作系统
开发环境:
VS2008,C++
四、实验步骤
1.FtpDlg.cpp
BOOLCFtpDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
m_strFtp=_T("");//初始化服务器域名
m_strName=_T("");//初始化登录用户名
m_strPwd=_T("");//初始化登录口令
UpdateData(FALSE);//更新界面
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCFtpDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCFtpDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCFtpDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
//当用户点击'查询'按钮,执行此函数。
voidCFtpDlg:
:
OnQuery()
{
CInternetSession*pSession;//定义会话对象指针变量
CFtpConnection*pConnection;//定义连接对象指针变量
CFtpFileFind*pFileFind;//定义文件查询对象指针变量
CStringstrFileName;
BOOLbContinue;
pConnection=NULL;//初始化
pFileFind=NULL;
UpdateData(TRUE);//获得用户的当前输入(服务器名,用户名和口令)
while(m_listFile.GetCount()!
=0)m_listFile.DeleteString(0);//清除列表框的内容
pSession=newCInternetSession(//创建Internet会话类对象
AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);
try
{//试图建立与指定FTP服务器的连接
pConnection=
pSession->GetFtpConnection(m_strFtp,m_strName,m_strPwd);
}catch(CInternetException*e){
e->Delete();//无法建立连接,进行错误处理
pConnection=NULL;
}
if(pConnection!
=NULL)
{
//创建CFtpFileFind对象,向构造函数传递CFtpConnection对象的指针
pFileFind=newCFtpFileFind(pConnection);
bContinue=pFileFind->FindFile("*");//查找服务器上当前目录的任意文件
if(!
bContinue)//如果一个文件都找不到,结束查找
{
pFileFind->Close();
pFileFind=NULL;
}
while(bContinue)//找到了第一个文件,继续找其它的文件
{
bContinue=pFileFind->FindNextFile();//查找下一个文件
strFileName=pFileFind->GetFileName();//获得找到的文件的文件名
//如果找到的是否目录,将目录名放在括弧中
if(pFileFind->IsDirectory())strFileName="["+strFileName+"]";
//将找到的文件或目录名显示在列表框中。
m_listFile.AddString(strFileName);
}
if(pFileFind!
=NULL)
{
pFileFind->Close();//结束查询
pFileFind=NULL;
}
}
deletepFileFind;//删除文件查询对象
if(pConnection!
=NULL)
{
pConnection->Close();
deletepConnection;//删除FTP连接对象
}
deletepSession;//删除Internet会话对象
}
//查询以后,当用户在列表框中选择了一个文件时,执行此函数。
voidCFtpDlg:
:
OnSelchangeListFile()
{
//禁用用来输入的文本框控件
m_editFtp.EnableWindow(FALSE);//服务器域名输入文本框
m_editName.EnableWindow(FALSE);//登录用户名输入文本框
m_editPwd.EnableWindow(FALSE);//口令输入文本框
m_staFtp.EnableWindow(FALSE);
m_staName.EnableWindow(FALSE);
m_staPwd.EnableWindow(FALSE);
//禁用查询和上传按钮
m_btnUpload.EnableWindow(FALSE);//上传按钮
m_btnQuery.EnableWindow(FALSE);//查询按钮
//激活下载按钮
m_btnDownload.EnableWindow(TRUE);
}
//当用户点击"下载"按钮时,执行此函数
voidCFtpDlg:
:
OnDownload()
{
//获得当前输入
UpdateData(TRUE);
intnSel=m_listFile.GetCurSel();
CStringstrSourceName;
//获得用户在列表框中的选择
nSel=m_listFile.GetCurSel();
m_listFile.GetText(nSel,strSourceName);
if(strSourceName.GetAt(0)!
='[')
{
//选择的是文件
CStringstrDestName;
CFileDialogdlg(FALSE,"","*.*");//定义了一个文件对话框对象变量
if(dlg.DoModal()==IDOK)//激活文件对话框
{
//获得下载文件在本地机上存储的路径和名称
strDestName=dlg.GetPathName();
//调用函数下载文件
if(Download(strSourceName,strDestName))
AfxMessageBox("下载成功!
",MB_OK|MB_ICONINFORMATION);
else
AfxMessageBox("下载失败!
",MB_OK|MB_ICONSTOP);
}else{
AfxMessageBox("请写入文件名!
",MB_OK|MB_ICONSTOP);
}
}else{
//选择的是目录
AfxMessageBox("不能下载目录!
\n请重选!
",MB_OK|MB_ICONSTOP);
}
//禁用下载按钮
m_btnDownload.EnableWindow(FALSE);
//激活查询和上传按钮
m_btnUpload.EnableWindow(TRUE);
m_btnQuery.EnableWindow(TRUE);
//激活用来输入的文本和编辑框控件
m_editFtp.EnableWindow(TRUE);
m_editName.EnableWindow(TRUE);
m_editPwd.EnableWindow(TRUE);
m_staFtp.EnableWindow(TRUE);
m_staName.EnableWindow(TRUE);
m_staPwd.EnableWindow(TRUE);
}
//Download函数由OnDownload函数调用,执行实际的下载任务
BOOLCFtpDlg:
:
Download(CStringstrSName,CStringstrDName)
{
CInternetSession*pSession;//定义会话对象变量指针
CFtpConnection*pConnection;//定义连接对象变量指针
pConnection=NULL;
//创建Internet会话对象
pSession=newCInternetSession(AfxGetAppName(),1,
PRE_CONFIG_INTERNET_ACCESS);
try
{
//建立FTP连接
pConnection=pSession->GetFtpConnection(m_strFtp,
m_strName,m_strPwd);
}
catch(CInternetException*e)
{
//错误处理
e->Delete();
pConnection=NULL;
returnFALSE;
}
if(pConnection!
=NULL)
{
//下载文件
if(!
pConnection->GetFile(strSName,strDName))
{
//下载文件错误
pConnection->Close();
deletepConnection;
deletepSession;
returnFALSE;
}
}
//清除对象
if(pConnection!
=NULL)
{
pConnection->Close();
deletepConnection;
}
deletepSession;
returnTRUE;
}
//当用户点击"上传"按钮时,执行此函数
voidCFtpDlg:
:
OnUpload()
{
//获得当前输入
UpdateData(TRUE);
//禁用用于输入的文本框控件
m_editFtp.EnableWindow(FALSE);//服务器域名输入文本框
m_editName.EnableWindow(FALSE);//登录客户名输入文本框
m_editPwd.EnableWindow(FALSE);//口令输入文本框
m_staFtp.EnableWindow(FALSE);
m_staName.EnableWindow(FALSE);
m_staPwd.EnableWindow(FALSE);
//禁用查询按钮
m_btnQuery.EnableWindow(FALSE);
CStringstrSourceName;
CStringstrDestName;
CFileDialogdlg(TRUE,"","*.*");//定义文本对话框对象变量
if(dlg.DoModal()==IDOK)
{
//获得待上传的本地机文件路径和文件名
strSourceName=dlg.GetPathName();
strDestName=dlg.GetFileName();
//调用Upload函数上传文件
if(Upload(strSourceName,strDestName))
AfxMessageBox("上传成功!
",MB_OK|MB_ICONINFORMATION);
else
AfxMessageBox("上传失败!
",MB_OK|MB_ICONSTOP);
}else{
//文件选择有错误
AfxMessageBox("请选择文件!
",MB_OK|MB_ICONSTOP);
}
//激活查询按钮
m_btnQuery.EnableWindow(TRUE);
//激活用于输入的文本框控件
m_editFtp.EnableWindow(TRUE);
m_editName.EnableWindow(TRUE);
m_editPwd.EnableWindow(TRUE);
m_staFtp.EnableWindow(TRUE);
m_staName.EnableWindow(TRUE);
m_staPwd.EnableWindow(TRUE);
}
//Upload函数由事件处理函数OnUpload调用,执行实际的上传任务
BOOLCFtpDlg:
:
Upload(CStringstrSName,CStringstrDName)
{
CInternetSession*pSession;
CFtpConnection*pConnection;
pConnection=NULL;
//创建Internet会话
pSession=newCInternetSession(AfxGetAppName(),1,
PRE_CONFIG_INTERNET_ACCESS);
try
{
//建立FTP连接
pConnection=pSession->GetFtpConnection(m_strFtp,
m_strName,m_strPwd);
}
catch(CInternetException*e)
{
//错误处理
e->Delete();
pConnection=NULL;
returnFALSE;
}
if(pConnection!
=NULL)
{
//上传文件
if(!
pConnection->PutFile(strSName,strDName))
{
//上传文件错误
pConnection->Close();
deletepConnection;
deletepSession;
returnFALSE;
}
}
//清除对象
if(pConnection!
=NULL)
{
pConnection->Close();
deletepConnection;
}
deletepSession;
returnTRUE;
}
五、实验结果与讨论
1.上传:
2.下载
六、总结
终于是做出来了,其中有老师的帮助,更少不了同学们的支持,谢谢你们
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 网络 编程 实验 ftp
![提示](https://static.bdocx.com/images/bang_tan.gif)