資源簡介
linux客戶端,實(shí)現(xiàn)上傳和下載功能,不支持?jǐn)帱c(diǎn)續(xù)傳
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?“damd_ftp.h“
#define?_DEBUG?0?//by?zhouq
bool?DAMD_FtpGetReply(FPT_FTP*?ftp)
{
char?recvbuf[FTP_BUFSIZE];
if?(ftp->CommandChunnel==INVALID_SOCKET)
{
printf(“Does?not?connect\n“);
return?false;
}
ftp->nReplyCode?=?0;
memset(ftp->szReplyString?0?FTP_BUFSIZE);
while?(DAMD_SockSelect(ftp->CommandChunnel?FD_READ?ftp->nMsTimeOut))
{
memset(recvbuf?0?FTP_BUFSIZE);
if?(DAMD_SockReceive(ftp->CommandChunnel?recvbuf?FTP_BUFSIZE-1)<=0)
{
printf(“Receive?reply?failed\n“);
return?false;
}
#ifdef?_DEBUG
printf(recvbuf);
#endif
??????
????if(strncmp(recvbuf“550“3)==0)
????{
???????printf(“?Failed?to?open?file.\n“);
????return?false;?//文件不存在
????}
if?(recvbuf[strlen(recvbuf)-1]==‘\n‘)
{
char*?lastline?=?recvbuf+strlen(recvbuf)-2;
for(;?*lastline!=‘\n‘?&&?lastline>recvbuf;?lastline--);
if?(*lastline==‘\n‘)?
lastline++;
if?(lastline[3]?==?‘?‘)
{
ftp->nReplyCode?=?atoi(lastline);
strcpy(ftp->szReplyString?lastline+4);
return?true;
}
}
}
return?false;
}
int?DAMD_FtpSendCommand(FPT_FTP*?ftp?const?char*?lpszCommand)
{
char?command[256];
if?(ftp->CommandChunnel==INVALID_SOCKET)
{
printf(“Does?not?connect\n“);
return?111;
}
#ifdef?_DEBUG
if?(strncmp(lpszCommand?“PASS?“?5)!=0)
printf(“CMD>%s\n“?lpszCommand);
else
printf(“CMD>PASS?*****\n“);
#endif
sprintf(command?“%s\r\n“?lpszCommand);
if?(DAMD_SockSend(ftp->CommandChunnel?command?strlen(command))<=0)
{
printf(“Send?command?‘%s‘?failed\n“?lpszCommand);
return?false;
}
return?DAMD_FtpGetReply(ftp);
}
FPT_FTP*?DAMD_FtpConnect(const?char*?lpszHostAddress?const?char*?lpszUser?const?char*?lpszPassword)
{
u_int?nFtpPort;
struct?protoent?*?lpProtocol;
char?szCommand[256];
FPT_FTP*?ftp?=?(FPT_FTP*)malloc(sizeof(FPT_FTP));
ftp->CommandChunnel?=?INVALID_SOCKET;
ftp->DataChunnel?=?INVALID_SOCKET;
ftp->pFtpList?=?NULL;
ftp->nFtpListCount?=?0;
ftp->nTransferMode?=?FPD_ModePassive;
ftp->nTransferType?=?FPD_TypeBinary;
ftp->nReplyCode?=?0;
ftp->nMsTimeOut?=?60000;
memset(ftp->szReplyString?0?FTP_BUFSIZE);
ftp->CommandChunnel?=?DAMD_SockCreate(0?SOCK_STREAM);
if?(ftp->CommandChunnel?==?INVALID_SOCKET)
{
printf(“Can?not?create?socket\n“);
free(ftp);
return?(ftp?=?NULL);
}
lpProtocol?=?getprotobyname(“ftp“);
if?(lpProtocol==NULL)
nFtpPort?=?21;
else
nFtpPort?=?lpProtocol->p_proto;
if?(!DAMD_SockConnect(ftp->CommandChunnel?lpszHostAddress?nFtpPort))
{
printf(“Connect?to?%s?failed\n“?lpszHostAddress);
free(ftp);
DAMD_SockClose(ftp->CommandChunnel);
return?(ftp?=?NULL);
}
DAMD_FtpGetReply(ftp);
if?(!lpszUser?||?!*lpszUser)
strcpy(szCommand?“USER?Anonymous“);
else
sprintf(szCommand?“USER?%s“?lpszUser);
if?
- 上一篇:100多個漂亮的ICO圖片,EPS格式!
- 下一篇:ELF文件格式詳解中文
評論
共有 條評論