/*******************************************************
Copyright 2014-2016 xhl Co., Ltd.
文 件: FTPManager.cpp
开发单位:
编 写: XHL
日 期: 2015.04
描 述: FTP文件上传下载管理文件
修 改:
********************************************************/
#include "stdafx.h"
#include "FTPManager.h"
static int SplitString( std::string strSrc, std::list<std::string> &strArray , std::string strFlag)
{
int pos = 1;
while((pos = (int)strSrc.find_first_of(strFlag.c_str())) > 0)
{
strArray.insert(strArray.end(), strSrc.substr(0 , pos));
strSrc = strSrc.substr(pos + 1, strSrc.length() - pos - 1);
}
strArray.insert(strArray.end(), strSrc.substr(0, strSrc.length()));
return 0;
}
CFTPManager::CFTPManager(void): m_bLogin(false)
{
m_cmdSocket = socket(AF_INET, SOCK_STREAM, 0);
}
CFTPManager::~CFTPManager(void)
{
std::string strCmdLine = parseCommand(FTP_COMMAND_QUIT, "");
Send(m_cmdSocket, strCmdLine.c_str());
closesocket(m_cmdSocket);
m_bLogin = false;
}
FTP_API CFTPManager::login2Server(const std::string &serverIP)
{
std::string strPort;
int pos = serverIP.find_first_of(":");
if (pos > 0)
{
strPort = serverIP.substr(pos + 1, serverIP.length() - pos);
}
else
{
pos = serverIP.length();
strPort = FTP_DEFAULT_PORT;
}
m_strServerIP = serverIP.substr(0, pos);
m_nServerPort = atol(strPort.c_str());
trace("IP: %s port: %d\n", m_strServerIP.c_str(), m_nServerPort);
if (Connect(m_cmdSocket, m_strServerIP, m_nServerPort) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
printf("@@@@Response: %s", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::login2Server(const std::string &serverIP,const std::string &serverPor)
{
std::string strPort;
int pos = serverIP.find_first_of(":");
if (pos > 0)
{
strPort = serverIP.substr(pos + 1, serverIP.length() - pos);
}
else
{
pos = serverIP.length();
strPort = serverPor;
}
m_strServerIP = serverIP.substr(0, pos);
m_nServerPort = atol(strPort.c_str());
trace("IP: %s port: %d\n", m_strServerIP.c_str(), m_nServerPort);
if (Connect(m_cmdSocket, m_strServerIP, m_nServerPort) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
printf("@@@@Response: %s", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::inputUserName(const std::string &userName)
{
std::string strCommandLine = parseCommand(FTP_COMMAND_USERNAME, userName);
m_strUserName = userName;
if (Send(m_cmdSocket, strCommandLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
printf("Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::inputPassWord(const std::string &password)
{
std::string strCmdLine = parseCommand(FTP_COMMAND_PASSWORD, password);
m_strPassWord = password;
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
else
{
m_bLogin = true;
m_strResponse = serverResponse(m_cmdSocket);
printf("Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
}
FTP_API CFTPManager::quitServer(void)
{
std::string strCmdLine = parseCommand(FTP_COMMAND_QUIT, "");
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
else
{
m_strResponse = serverResponse(m_cmdSocket);
printf("Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
}
const std::string CFTPManager::PWD()
{
std::string strCmdLine = parseCommand(FTP_COMMAND_CURRENT_PATH, "");
if (Send(m_cmdSocket, strCmdLine.c_str()) < 0)
{
return "";
}
else
{
return serverResponse(m_cmdSocket);
}
}
FTP_API CFTPManager::setTransferMode(type mode)
{
std::string strCmdLine;
switch (mode)
{
case binary:
strCmdLine = parseCommand(FTP_COMMAND_TYPE_MODE, "I");
break;
case ascii:
strCmdLine = parseCommand(FTP_COMMAND_TYPE_MODE, "A");
break;
default:
break;
}
if (Send(m_cmdSocket, strCmdLine.c_str()) < 0)
{
assert(false);
}
else
{
m_strResponse = serverResponse(m_cmdSocket);
printf("@@@@Response: %s", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
}
const std::string CFTPManager::Pasv()
{
std::string strCmdLine = parseCommand(FTP_COMMAND_PSAV_MODE, "");
if (Send(m_cmdSocket, strCmdLine.c_str()) < 0)
{
return "";
}
else
{
m_strResponse = serverResponse(m_cmdSocket);
return m_strResponse;
}
}
const std::string CFTPManager::Dir(const std::string &path)
{
int dataSocket = socket(AF_INET, SOCK_STREAM, 0);
if (createDataLink(dataSocket) < 0)
{
return "";
}
// 数据连接成功
std::string strCmdLine = parseCommand(FTP_COMMAND_DIR, path);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
trace("@@@@Response: %s\n", serverResponse(m_cmdSocket).c_str());
closesocket(dataSocket);
return "";
}
else
{
trace("@@@@Response: %s\n", serverResponse(m_cmdSocket).c_str());
m_strResponse = serverResponse(dataSocket);
trace("@@@@Response: \n%s\n", m_strResponse.c_str());
closesocket(dataSocket);
return m_strResponse;
}
}
FTP_API CFTPManager::CD(const std::string &path)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_CHANGE_DIRECTORY, path);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
trace("@@@@Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::DeleteFile(const std::string &strRemoteFile)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_DELETE_FILE, strRemoteFile);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
printf("@@@@Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::DeleteDirectory(const std::string &strRemoteDir)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_DELETE_DIRECTORY, strRemoteDir);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
trace("@@@@Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::CreateDirectory(const std::string &strRemoteDir)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_CREATE_DIRECTORY, strRemoteDir);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
trace("@@@@Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
FTP_API CFTPManager::Rename(const std::string &strRemoteFile, const std::string &strNewFile)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_RENAME_BEGIN, strRemoteFile);
Send(m_cmdSocket, strCmdLine);
trace("@@@@Response: %s\n", serverResponse(m_cmdSocket).c_str());
Send(m_cmdSocket, parseCommand(FTP_COMMAND_RENAME_END, strNewFile));
m_strResponse = serverResponse(m_cmdSocket);
trace("@@@@Response: %s\n", m_strResponse.c_str());
return parseResponse(m_strResponse);
}
long CFTPManager::getFileLength(const std::string &strRemoteFile)
{
assert(m_cmdSocket != INVALID_SOCKET);
std::string strCmdLine = parseCommand(FTP_COMMAND_FILE_SIZE, strRemoteFile);
if (Send(m_cmdSocket, strCmdLine) < 0)
{
return -1;
}
m_strResponse = serverResponse(m_cmdSocket);
trace("@@@@Response: %s\n", m_strResponse.c_str());
std::string strData = m_strResponse.substr(0, 3);
unsigned long val = atol(strData.c_str());
if (val == 213)
{