- 论坛徽章:
- 0
|
我在linux下用socket实现ftp客户端,对于list这个命令,我代码实现如下
int uc_FtpClient::ListDir()
{
bool bRet = ftpListen();//实现数据链路的监听
strCommand = "LIST\r\n";
bRet = SendCommand( strCommand);
int nRet = RecvCommandEcho(strMsg); //问题出现在这,
//strMsg是:“150 here comes the directory listing.\r\n226 Directory send ok\r\n"
std::cout << strMsg << std::endl; //但前两三次,
//这里显示:150 here comes the directory listing.\r\n.
//226 Directory send ok\r\n 没有显示,两三次后,这句也出现
bRet = ftpCreateDataChannel();//创建数据传输通道
long recv_size = RecvData( cBuf, MAX_FILE_LEN );//接收传回来的数据
std::cout << cBuf << std::endl; //显示接收到的目录信息
ftpCloseDataListen(); //数据传送通道关闭
RecvCommandEcho( strMsg ); //再次接收一次命令返回码
//前两三次,因为,上面的RecvCommandEcho( strMsg );只接收到一句,这句运行良好
//显示:226 Directory send ok\r\n
//而且没这一句,第二次运行此函数时,首先接收到的是//226 Directory send ok\r\n
//并且程序不再正常运行
//有这句,两三次后,这句在死等,无法继续
}
bool uc_FtpClient::SendCommand(string strCommand)
{
int bytes_send = send(m_controlSockhd, pcComman, nLen, 0);
if ( bytes_send != nLen )
{
perror("Send command error:");
return false;
}
return true;
}
int uc_FtpClient::RecvCommandEcho(string strMsg)
{
int bytes_recv = recv(m_controlSockhd, cMsg, MAX_COMMAND_LEN, 0);
if ( 0 >= bytes_recv )
{
perror( "recv command echo error:");
return -1;
}
return bytes_recv;
}
这是我大概的程序,问题在程序中说明,哪位大侠指点一二,谢谢!急 |
|