如何在unix下,c语言中实现ftp文件传输

 我来答
匿名用户
2017-01-11
展开全部
nt FtpInit(char* Host,char* Account,char* Passwd)
{
short shPortNumber;
long lAddr;
char RecvBuf[1024];
char SendBuf[1024];
int RecvLen,SendLen;

shPortNumber=htons(21);
lAddr=inet_addr(Host);
memset(HostName,0,16);
memcpy(HostName,Host,strlen(Host));
hClient=socket(AF_INET,SOCK_STREAM,0);
if (hClient<0)
{
return -1;
}
if(setsockopt(hClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&rcvtime,sizeof(int)))
{
close(hClient);
return -1;
}

if(setsockopt(hClient,SOL_SOCKET,SO_KEEPALIVE,(char *)&keepalive,sizeof(int)))
{
close(hClient);
return -1;
}
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = lAddr;
SockAddr.sin_port = shPortNumber;

if (connect(hClient,(const struct sockaddr *)&SockAddr,sizeof(SockAddr))<0)
{
close(hClient);
return -1;
}

memset(RecvBuf,0,1024);
if((RecvLen=FtpMatchReceive(hClient,RecvBuf, "220 ",1024))<=0)
{
close(hClient);
return -1;
}
if(SendLen=GetFtpSendBuf("USER",Account,SendBuf,1024)<=0)
{
close(hClient);
return -1 ;
}
if(SendFTPCommand(hClient,SendBuf)!=331)
{
close(hClient);
return -1;
}
if(SendLen=GetFtpSendBuf("PASS",Passwd,SendBuf,1024)<=0)
{
close(hClient);
return -1 ;
}
if(SendFTPCommand(hClient,SendBuf)!=230)
{
close(hClient);
return -1;
}

}
int FtpGetFile(char * FileName)
{
int hListenSocket;
int hDataSocket;
int RetWriteFile;
int ReturnCode;
char RemoteFileName[256];
char RemoteFile[256];
char ExecCommand[256];
char * pcSubDir;

memset(RemoteFileName,0,256);
memset(RemoteFile,0,256);
memset(ExecCommand,0,256);

strcpy(RemoteFileName,FileName);
memcpy(ExecCommand,"CWD ",4);

pcSubDir=strrchr(RemoteFileName,'\\');
if (pcSubDir !=NULL)
{
strncat(ExecCommand,RemoteFileName,RemoteFileName-pcSubDir);
if(SendFTPCommand(hClient,ExecCommand)!=250)
{
close(hClient);
return -1;
}
strcpy(RemoteFile,pcSubDir);
}
else
{
strcpy(RemoteFile,RemoteFileName);
}

if((hListenSocket=CreatListenSocket(hClient))<0)
{
close(hClient);
return -1;
}
if(RequestDataConnection(hClient,hListenSocket)<0)
{
close(hClient);
return -1;
}
memset( ExecCommand,0,256);
memcpy( ExecCommand,"RETR ",5);
strcat( ExecCommand,RemoteFile);
printf("The FileName=%s\n",RemoteFile);
strcat( ExecCommand,"\r\n");

ReturnCode=SendFTPCommand(hClient,ExecCommand);
if(ReturnCode!=150)
{
close(hClient);
return -1;
}
if((hDataSocket=AcceptDataConnection(hListenSocket))<0)
{
close(hClient);
return -1;
}
if((RetWriteFile=WriteFile(hDataSocket,RemoteFile))<0)
{
close(hDataSocket);
close(hClient);
return -1;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式