if ((ReadResp('2', struFtpPara)) != FTP_SUCCESS) // 应答 220这个是神马意思 尤其是里面的 2int CFtp::Connect(const char *host, const unsigned short portnumber)
{
if (struFtpPara == NULL)
{
return FTP_INIT_FAILED;
}
if ((host == NULL) || (portnumber == 0))
return FTP_GENERIC_ERROR;
int sControl;
struct sockaddr_in sin;
struct hostent *phe;
// struct servent *pse;
int on = 1;
int ret = 0; struFtpPara->conn = FTP_CONTROL;
struFtpPara->ctrl = NULL; struFtpPara->offset = 0;
struFtpPara->handle = 0; memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(portnumber); if ((sin.sin_addr.s_addr = inet_addr(host)) == -1) //lint !e650
{
if ((phe = gethostbyname(host)) == NULL)
{
return FTP_GET_SERVER_IP_FAILED;
}
memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
} sControl = (int)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sControl == -1)
return FTP_SOCKET_ERROR; // 开启 允许重用本地地址 选项
if (setsockopt(sControl, SOL_SOCKET, SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
} // 设置发送超时时间为10秒
struct timeval timeo;
timeo.tv_sec = 10;
timeo.tv_usec = 0; int iRst = setsockopt(sControl, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeo, sizeof(timeo));
if (iRst < 0)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
} if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
net_close(sControl);
return FTP_CONNECT_REFUSED;
} // 设置接收超时时间为10秒
iRst = setsockopt(sControl, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeo, sizeof(timeo));
if (iRst < 0)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
}
struFtpPara->handle = sControl; if ((ReadResp('2', struFtpPara)) != FTP_SUCCESS) // 应答 220
{
net_close(sControl);
struFtpPara->handle = 0;
return FTP_INVALID_REPLY_FROM_SERVER;
} return FTP_SUCCESS;
}
{
if (struFtpPara == NULL)
{
return FTP_INIT_FAILED;
}
if ((host == NULL) || (portnumber == 0))
return FTP_GENERIC_ERROR;
int sControl;
struct sockaddr_in sin;
struct hostent *phe;
// struct servent *pse;
int on = 1;
int ret = 0; struFtpPara->conn = FTP_CONTROL;
struFtpPara->ctrl = NULL; struFtpPara->offset = 0;
struFtpPara->handle = 0; memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(portnumber); if ((sin.sin_addr.s_addr = inet_addr(host)) == -1) //lint !e650
{
if ((phe = gethostbyname(host)) == NULL)
{
return FTP_GET_SERVER_IP_FAILED;
}
memcpy((char *)&sin.sin_addr, phe->h_addr, phe->h_length);
} sControl = (int)socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sControl == -1)
return FTP_SOCKET_ERROR; // 开启 允许重用本地地址 选项
if (setsockopt(sControl, SOL_SOCKET, SO_REUSEADDR, SETSOCKOPT_OPTVAL_TYPE &on, sizeof(on)) == -1)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
} // 设置发送超时时间为10秒
struct timeval timeo;
timeo.tv_sec = 10;
timeo.tv_usec = 0; int iRst = setsockopt(sControl, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeo, sizeof(timeo));
if (iRst < 0)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
} if (connect(sControl, (struct sockaddr *)&sin, sizeof(sin)) == -1)
{
net_close(sControl);
return FTP_CONNECT_REFUSED;
} // 设置接收超时时间为10秒
iRst = setsockopt(sControl, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeo, sizeof(timeo));
if (iRst < 0)
{
net_close(sControl);
return FTP_SOCKET_ERROR;
}
struFtpPara->handle = sControl; if ((ReadResp('2', struFtpPara)) != FTP_SUCCESS) // 应答 220
{
net_close(sControl);
struFtpPara->handle = 0;
return FTP_INVALID_REPLY_FROM_SERVER;
} return FTP_SUCCESS;
}
connect函数调用前的setsockopt操作,都是设置当前Socket的工作参数(发送,接收超时等待的时间)。
至于那个2,是当connect成功时,服务器端会先发送一条220开头的状态码给客户端,之后再根据FTP的请求做进一步的会话操作。
建议楼主先了解下FTP的协议内容,里面都有FTP的状态码解释以及详细的FTP工作流程介绍。