我帮人写一个FTP的客户端,可是写到显示服务器目录的时候写不下去了,因为编译没错,但是就是执行不了,我把代码帖出来,大家看看为啥啊,急着用,大家行行好吧
// FTPClientDlg.cpp : implementation file
//#include "stdafx.h"
#include "FTPClient 1.h"
#include "FTPClientDlg.h"#include <winsock2.h>
#include"afxinet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#pragma comment(lib,"ws2_32.lib") //导入ws2_32.lib库/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg dialogCFTPClientDlg::CFTPClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFTPClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFTPClientDlg)
m_user = _T("");
m_password = _T("");
m_port = 21;
m_localdir = _T("");
m_pconnect = NULL;
m_pFileFind = NULL;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CFTPClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFTPClientDlg)
DDX_Control(pDX, IDC_SERDIR, m_FtpFile);
DDX_Control(pDX, IDC_IPADDRESS, m_ftpaddr);
DDX_Text(pDX, IDC_USER, m_user);
DDX_Text(pDX, IDC_PASSWORD, m_password);
DDX_Text(pDX, IDC_PORT, m_port);
DDX_Text(pDX, IDC_LOCALDIR, m_localdir);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CFTPClientDlg, CDialog)
//{{AFX_MSG_MAP(CFTPClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_QUARY, OnQuary)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg message handlersBOOL CFTPClientDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//初始化操作
CString strIP="127.0.0.1";
DWORD dwAddress= ntohl(inet_addr(strIP));
m_ftpaddr.SetAddress(dwAddress); m_FtpFile.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
//设置列
m_FtpFile.InsertColumn(0,"文件名",LVCFMT_CENTER,200);
m_FtpFile.InsertColumn(1,"日期",LVCFMT_CENTER,100);
m_FtpFile.InsertColumn(2,"字节数",LVCFMT_CENTER,100);
//初如化CftpFileFind类对象m_pFileFind
m_pFileFind = new CFtpFileFind(m_pconnect); //调用OnQuary函数查询FTP服务器当前目录下的目录和文件信息
OnQuary();
return TRUE; // return TRUE unless you set the focus to a control
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CFTPClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFTPClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CFTPClientDlg::ListContent(LPCTSTR DirName)
{
m_FtpFile.DeleteAllItems();
BOOL bContinue;
bContinue=m_pFileFind->FindFile(DirName);
if (!bContinue)
{
//查找完毕,失败
m_pFileFind->Close();
m_pFileFind=NULL;
}
CString strFileName;
CString strFileTime;
CString strFileLength;
while (bContinue)
{
bContinue = m_pFileFind->FindNextFile();
strFileName = m_pFileFind->GetFileName(); //得到文件名
//得到文件最后一次修改的时间
FILETIME ft;
m_pFileFind->GetLastWriteTime(&ft);
CTime FileTime(ft);
strFileTime = FileTime.Format("%y/%m/%d");
if (m_pFileFind->IsDirectory())
{
//如果是目录不求大小,用<DIR>代替
strFileLength = "<DIR>";
}
else
{
//得到文件大小
if (m_pFileFind->GetLength64() <1024)
{
strFileLength.Format("%d B",m_pFileFind->GetLength64());
}
else
{
if (m_pFileFind->GetLength64() < (1024*1024))
strFileLength.Format("%3.3f KB",
(LONGLONG)m_pFileFind->GetLength64()/1024.0);
else
{
if (m_pFileFind->GetLength64()<(1024*1024*1024))
strFileLength.Format("%3.3f MB",
(LONGLONG)m_pFileFind->GetLength64()/(1024*1024.0));
else
strFileLength.Format("%1.3f GB",
(LONGLONG)m_pFileFind->GetLength64()/(1024.0*1024*1024));
}
}
}
int i=0;
m_FtpFile.InsertItem(i,strFileName,0);
m_FtpFile.SetItemText(i,1,strFileTime);
m_FtpFile.SetItemText(i,2,strFileLength);
i++;
}
}
void CFTPClientDlg::OnConnect()
{//连接ftp服务器
// TODO: Add your control notification handler code here
unsigned char *pIP;
CString strIP,errorstring;
DWORD dwIP;
m_ftpaddr.GetAddress(dwIP);
pIP = (unsigned char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3), *(pIP+2), *(pIP+1), *pIP);//得到服务器地址
CInternetSession *pftp=new CInternetSession(AfxGetAppName(),
1,
PRE_CONFIG_INTERNET_ACCESS);
try{
//试图建立FTP连接
SetTimer(1,1000,NULL); //设置定时器,一秒发一次WM_TIMER
m_localdir="正在连接中....";
m_pconnect=pftp->GetFtpConnection(strIP,m_user,m_password,m_port);
m_localdir="连接成功!";
//m_pconnect->SetCurrentDirectory();
}
catch(CInternetException* pEx)
{
TCHAR szErr[1024];
if (pEx->GetErrorMessage(szErr, 1024))
{
m_pconnect = NULL;
errorstring.Empty();
errorstring+="连接字符有误! \n";
errorstring+=szErr;
}
} OnQuary();
//m_localdir=errorstring;
UpdateData(false);
}/*void CMyFtpView::OnConnect()
{
// TODO: Add your command handler code here
//生成一个模态对话框
if (IDOK==m_ConDlg.DoModal())
{
m_pConnection = NULL;
m_pSession = NULL;
m_FtpWebSite = m_ConDlg.m_FtpWebSite;
m_UserName = m_ConDlg.m_UserName;
m_UserPwd = m_ConDlg.m_UserPwd;
m_pSession=new CInternetSession(AfxGetAppName(),
1,
PRE_CONFIG_INTERNET_ACCESS);
try
{
//试图建立FTP连接
SetTimer(1,1000,NULL); //设置定时器,一秒发一次WM_TIMER
CString str="正在连接中....";
((CMainFrame*)GetParent())->SetMessageText(str);
m_pConnection=m_pSession->GetFtpConnection(m_FtpWebSite,
m_UserName,m_UserPwd);
}
catch (CInternetException* e)
{
//错误处理
e->Delete();
m_pConnection=NULL;
}
}
}
*/void CFTPClientDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int time_out=1;
time_out++;
if (m_pconnect == NULL)
{
m_localdir="正在连接中....";
if (time_out>=60)
{
KillTimer(1);
MessageBox("连接超时!","超时",MB_OK);
}
}
else
{
m_localdir="连接成功!";
KillTimer(1);
}
CDialog::OnTimer(nIDEvent);
}void CFTPClientDlg::OnQuary()
{
// TODO: Add your control notification handler code here
ListContent("*");
}
下面是错误截图:
// FTPClientDlg.cpp : implementation file
//#include "stdafx.h"
#include "FTPClient 1.h"
#include "FTPClientDlg.h"#include <winsock2.h>
#include"afxinet.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif#pragma comment(lib,"ws2_32.lib") //导入ws2_32.lib库/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg dialogCFTPClientDlg::CFTPClientDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFTPClientDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFTPClientDlg)
m_user = _T("");
m_password = _T("");
m_port = 21;
m_localdir = _T("");
m_pconnect = NULL;
m_pFileFind = NULL;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}void CFTPClientDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFTPClientDlg)
DDX_Control(pDX, IDC_SERDIR, m_FtpFile);
DDX_Control(pDX, IDC_IPADDRESS, m_ftpaddr);
DDX_Text(pDX, IDC_USER, m_user);
DDX_Text(pDX, IDC_PASSWORD, m_password);
DDX_Text(pDX, IDC_PORT, m_port);
DDX_Text(pDX, IDC_LOCALDIR, m_localdir);
//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CFTPClientDlg, CDialog)
//{{AFX_MSG_MAP(CFTPClientDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_QUARY, OnQuary)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CFTPClientDlg message handlersBOOL CFTPClientDlg::OnInitDialog()
{
CDialog::OnInitDialog(); // Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//初始化操作
CString strIP="127.0.0.1";
DWORD dwAddress= ntohl(inet_addr(strIP));
m_ftpaddr.SetAddress(dwAddress); m_FtpFile.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
//设置列
m_FtpFile.InsertColumn(0,"文件名",LVCFMT_CENTER,200);
m_FtpFile.InsertColumn(1,"日期",LVCFMT_CENTER,100);
m_FtpFile.InsertColumn(2,"字节数",LVCFMT_CENTER,100);
//初如化CftpFileFind类对象m_pFileFind
m_pFileFind = new CFtpFileFind(m_pconnect); //调用OnQuary函数查询FTP服务器当前目录下的目录和文件信息
OnQuary();
return TRUE; // return TRUE unless you set the focus to a control
}// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.void CFTPClientDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFTPClientDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}void CFTPClientDlg::ListContent(LPCTSTR DirName)
{
m_FtpFile.DeleteAllItems();
BOOL bContinue;
bContinue=m_pFileFind->FindFile(DirName);
if (!bContinue)
{
//查找完毕,失败
m_pFileFind->Close();
m_pFileFind=NULL;
}
CString strFileName;
CString strFileTime;
CString strFileLength;
while (bContinue)
{
bContinue = m_pFileFind->FindNextFile();
strFileName = m_pFileFind->GetFileName(); //得到文件名
//得到文件最后一次修改的时间
FILETIME ft;
m_pFileFind->GetLastWriteTime(&ft);
CTime FileTime(ft);
strFileTime = FileTime.Format("%y/%m/%d");
if (m_pFileFind->IsDirectory())
{
//如果是目录不求大小,用<DIR>代替
strFileLength = "<DIR>";
}
else
{
//得到文件大小
if (m_pFileFind->GetLength64() <1024)
{
strFileLength.Format("%d B",m_pFileFind->GetLength64());
}
else
{
if (m_pFileFind->GetLength64() < (1024*1024))
strFileLength.Format("%3.3f KB",
(LONGLONG)m_pFileFind->GetLength64()/1024.0);
else
{
if (m_pFileFind->GetLength64()<(1024*1024*1024))
strFileLength.Format("%3.3f MB",
(LONGLONG)m_pFileFind->GetLength64()/(1024*1024.0));
else
strFileLength.Format("%1.3f GB",
(LONGLONG)m_pFileFind->GetLength64()/(1024.0*1024*1024));
}
}
}
int i=0;
m_FtpFile.InsertItem(i,strFileName,0);
m_FtpFile.SetItemText(i,1,strFileTime);
m_FtpFile.SetItemText(i,2,strFileLength);
i++;
}
}
void CFTPClientDlg::OnConnect()
{//连接ftp服务器
// TODO: Add your control notification handler code here
unsigned char *pIP;
CString strIP,errorstring;
DWORD dwIP;
m_ftpaddr.GetAddress(dwIP);
pIP = (unsigned char*)&dwIP;
strIP.Format("%u.%u.%u.%u",*(pIP+3), *(pIP+2), *(pIP+1), *pIP);//得到服务器地址
CInternetSession *pftp=new CInternetSession(AfxGetAppName(),
1,
PRE_CONFIG_INTERNET_ACCESS);
try{
//试图建立FTP连接
SetTimer(1,1000,NULL); //设置定时器,一秒发一次WM_TIMER
m_localdir="正在连接中....";
m_pconnect=pftp->GetFtpConnection(strIP,m_user,m_password,m_port);
m_localdir="连接成功!";
//m_pconnect->SetCurrentDirectory();
}
catch(CInternetException* pEx)
{
TCHAR szErr[1024];
if (pEx->GetErrorMessage(szErr, 1024))
{
m_pconnect = NULL;
errorstring.Empty();
errorstring+="连接字符有误! \n";
errorstring+=szErr;
}
} OnQuary();
//m_localdir=errorstring;
UpdateData(false);
}/*void CMyFtpView::OnConnect()
{
// TODO: Add your command handler code here
//生成一个模态对话框
if (IDOK==m_ConDlg.DoModal())
{
m_pConnection = NULL;
m_pSession = NULL;
m_FtpWebSite = m_ConDlg.m_FtpWebSite;
m_UserName = m_ConDlg.m_UserName;
m_UserPwd = m_ConDlg.m_UserPwd;
m_pSession=new CInternetSession(AfxGetAppName(),
1,
PRE_CONFIG_INTERNET_ACCESS);
try
{
//试图建立FTP连接
SetTimer(1,1000,NULL); //设置定时器,一秒发一次WM_TIMER
CString str="正在连接中....";
((CMainFrame*)GetParent())->SetMessageText(str);
m_pConnection=m_pSession->GetFtpConnection(m_FtpWebSite,
m_UserName,m_UserPwd);
}
catch (CInternetException* e)
{
//错误处理
e->Delete();
m_pConnection=NULL;
}
}
}
*/void CFTPClientDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
static int time_out=1;
time_out++;
if (m_pconnect == NULL)
{
m_localdir="正在连接中....";
if (time_out>=60)
{
KillTimer(1);
MessageBox("连接超时!","超时",MB_OK);
}
}
else
{
m_localdir="连接成功!";
KillTimer(1);
}
CDialog::OnTimer(nIDEvent);
}void CFTPClientDlg::OnQuary()
{
// TODO: Add your control notification handler code here
ListContent("*");
}
下面是错误截图:
//初如化CftpFileFind类对象m_pFileFind
m_pFileFind = new CFtpFileFind(m_pconnect);
不应该在OnInitDialog() 里面做,而应该在OnConnect()里面做,因为那是m_pconnect才有有效值,呵呵