FTP的LIST命令在网络上已经有了成型的代码,怎么到我这就运行不过去呢?
运行后出现的问题
1,得到的文件大小不正确
2,出现内存泄露
主要是用LIST得到远地服务器上边的文件的大小,函数内容如下:
ULONGLONG CFTPClass::GetFTPFileLength(CString filename)
{
CString rmFileDir, rmFileName, strTemp, strFileName;
BOOL bDirectory = 1;
char szrmFile[_MAX_PATH];
int nIndex = 0;
rmFileDir = "";
memset(szrmFile, 0, _MAX_PATH);
strncpy(szrmFile, filename, filename.GetLength());
if(List(filename))
return -1;
TRACE("%s",m_retmsg);
CString strFileList;
int i = 0;
int nSize = m_buf.GetSize();
char* fileString = new char[nSize];
for(i = 0; i < nSize; i++){
fileString[i] = m_buf.GetAt(i);
}
strFileList = fileString;
TRACE("%s",strFileList);
delete [] fileString;
nIndex = 0;
if((nIndex = strFileList.Find("Jan")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Feb")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Mar")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Apr")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("May")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Jun")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Jul")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Aug")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Sep")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Oct")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Nov")) != -1)
goto ExitFind;
else if((nIndex = strFileList.Find("Dec")) != -1)
goto ExitFind;ExitFind:
strTemp = strFileList.Left(nIndex - 1);
while(1){
nIndex = strTemp.Find(" ", 0);
strTemp.Delete(0, nIndex + 1);
if(nIndex == -1)
break;
}
if(strTemp == "")
return 0;
else
return (atol(strTemp));
}
BOOL CFTPClass::List(CString str)
{
CString lhost,temp,rhost;
UINT localsock = 0,i;
CSocket sockServr;
CAsyncSocket datachannel;
int num = 0,sum;
const int BUFSIZE =4096;
DWORD lpArgument = 0;
m_buf.RemoveAll();
m_buf.SetSize(BUFSIZE);
if(!FTPCommand("TYPE I"))
return false;
m_retmsg = "正在得到本地ip";
if(!m_Ctrlsok->GetSockName(lhost,localsock))
return false;
while(1)
{
if((i=lhost.Find("."))==-1)
break;
lhost.SetAt(i,',');
}
if((!sockServr.Create(0,SOCK_STREAM,NULL))||(!sockServr.Listen()))
return false;
if(!sockServr.GetSockName(temp,localsock))
return false;
lhost.Format(lhost+",%d,%d",localsock/256,localsock%256);
if(!FTPCommand("PORT "+lhost))
return false;
if(!WriteStr("LIST "+str))
return false;
if(!ReadStr())
return false;
if(this->m_retmsg.Find("226") == -1)
{
//LIST命令接收2行服务器响应
if(!FTPCommand(""))
{
sockServr.ShutDown(2);
sockServr.Close();
return false;
}
}
else
{
this->m_fc = 2;
}
if(this->m_fc == 4)
{
sockServr.ShutDown(2);
sockServr.Close();
return false;
}
if(!sockServr.Accept(datachannel))
return false;
if((!datachannel.AsyncSelect(0))||(!datachannel.IOCtl(FIONBIO,&lpArgument)))
{
m_retmsg = "出现错误";
return false;
}
sum = 0;
while(1)
{
TRY
{
if(!(num = datachannel.Receive(m_buf.GetData()+sum,BUFSIZE,0))||num ==SOCKET_ERROR)
break;
TRACE("Received : %d",num);
Sleep(0);
sum+=num;
m_buf.SetSize(sum+BUFSIZE);
}
CATCH(CException,e)
{
m_retmsg = "出现异常";
return false;
}
END_CATCH
}
datachannel.Close();
}

解决方案 »

  1.   

    在ExitFind:
    strTemp = strFileList.Left(nIndex - 1);
    处夹断点,调试,查看最终分离的strTemp是不是个正确的数字串,是不是中间有,格开的至于内存泄漏,可以根据输出的调试信息确定位置
      

  2.   

    得到的消息正确,但是没返回文件信息,我怀疑自己的程序发给服务器的ftp命令的顺序可能有问题
    附内存泄露的调试信息
    Detected memory leaks!
    Dumping objects ->
    arccore.cpp(435) : {252} normal block at 0x00379380, 4096 bytes long.
     Data: <REST 0    indows> 52 45 53 54 20 30 0D 0A 0D 0A 69 6E 64 6F 77 73 
    {251} normal block at 0x003792F8, 72 bytes long.
     Data: <                > 00 00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 
    arccore.cpp(435) : {250} normal block at 0x003782B8, 4096 bytes long.
     Data: <                > 0A CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
    {249} normal block at 0x00378230, 72 bytes long.
     Data: <                > 00 00 00 00 01 00 00 00 00 00 00 00 02 00 00 00 
    {248} client block at 0x00377E80, subtype c0, 24 bytes long.
    a CSocketFile object at $00377E80, 24 bytes long
    {240} client block at 0x00377EE0, subtype c0, 20 bytes long.
    a CSocket object at $00377EE0, 20 bytes long
    Object dump complete.
    程序“[3500] test.exe: 本机”已退出,返回值为 0 (0x0)。