如何在unix下,c语言中实现ftp文件传输
1个回答
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;
}
}
{
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询