📄 ftp_source.c
字号:
}*/
static void SetDataDirectory(char *dir)
{
if (chdir(dir))
{
return;
}
mkdir(dir,NULL);
}
static int 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)
{
printf("hClient<0\n");
return -1;
}
/* if(setsockopt(hClient,SOL_SOCKET,SO_RCVTIMEO,(char *)&rcvtime,sizeof(int)))
//if(setsockopt(hClient,SOL_SOCKET,SO_RCVTIMEO,(char *)NULL,sizeof(int)))
{
printf("setsockopt 1\n");
close(hClient);
return -1;
}*/
if(setsockopt(hClient,SOL_SOCKET,SO_KEEPALIVE,(char *)&keepalive,sizeof(int)))
//if(setsockopt(hClient,SOL_SOCKET,SO_KEEPALIVE,(char *)NULL,sizeof(int)))
{
printf("setsockopt 2\n");
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;
}
SendFTPCommand(hClient,"TYPE I\r\n");
}
static int FtpGetFile(char * FileName, char * localFileName )
{
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)
{
printf("FtpGetFile err 1\n");
close(hClient);
return -1;
}
strcpy(RemoteFile,pcSubDir);
}
else
{
strcpy(RemoteFile,RemoteFileName);
}
if((hListenSocket=CreatListenSocket(hClient))<0)
{
printf("FtpGetFile err 2\n");
close(hClient);
return -1;
}
if( RequestDataConnection(hClient,hListenSocket)<0 )
{
printf("FtpGetFile err 3\n");
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,localFileName ))<0)
{
close(hDataSocket);
close(hClient);
return -1;
}
}
static int FtpPutFile( char * localFileName, 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,"STOR ",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=WriteRemoteFile(hDataSocket,localFileName))<0)
{
close(hDataSocket);
close(hClient);
return -1;
}
}
int ftpGet( char * ip, char * user, char * password, char * RemoteFileName, char * localFileName )
{
int result = -1;
if ( FtpInit( ip, user, password ) != -1 )
{
if ( FtpGetFile( RemoteFileName, localFileName ) == -1 )
{
printf("FtpGetFile error!\n" );
}
else
{
printf("download file success\n");
result = 0;
}
FtpClose();
}
else
printf("FtpInit error!\n" );
return result;
}
int ftpPut( char * ip, char * user, char * password, char * localFileName, char * RemoteFileName )
{
int result = -1;
if ( FtpInit( ip, user, password ) != -1 )
{
if ( FtpPutFile( localFileName, RemoteFileName ) == -1 ) printf("FtpPutFile error!\n" );
FtpClose();
result = 0;
}
else
printf("FtpInit error!\n" );
return result;
}
int ftpGetOk()
{
return 1;
}
int ftpMoudle()
{
return 0;
}
#ifndef __FTPMOUDLE_H
#define __FTPMOUDLE_H
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/stat.h>
#include <math.h>
#include <time.h>
#include <signal.h>
int ftpGet( char * ip, char * user, char * password, char * RemoteFileName, char * localFileName );
int ftpPut( char * ip, char * user, char * password, char * localFileName, char * RemoteFileName );
int ftpGetOk();
int ftpMoudle();
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -