FTP 上传下载,多进程,多线程上传(1)

来源:互联网 发布:python 爬知乎 编辑:程序博客网 时间:2024/06/08 08:54

//ftp_client.h

#ifndef _FTP_CLIENT
#define _FTP_CLIENT

#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <pthread.h>
using namespace std;


#define UNIX_SPACE ' '
#define UNIX_TABLESPACE '/t'
#define UNIX_NEWLINE '/n'
#define LEFT_SIGN '['
#define RIGHT_SIGN ']'
#define DATA_TRANS_SUCC "226"
#define WAIT_TIMES 50
#define DEF_BUFFER_SIZE 4096
#define PROC_COUNT 5

static char CONFIG_FILE_PATH[]="./configfile.txt";
static char LIST_FILE_PATH[]="./listfile.txt";
enum FTP_CMD {USER, PASS, PWD, TYPE, PORT, LIST, CWD, SIZE, RETR, REST, PRETR, TRETR, STOR, QUIT};

typedef int(*PFV)(char*, char*);

struct Login_Infor
{
    char user[20];
    char password[20];
    char serverIp[20];
    int  serverPort;
    char type;
    char serverPath[50];
    char localIp[20];
    int  localPort; 
    char localPath[50];
};

struct Load_Task
{
    pid_t pid;
    int   state;
    int   offset;
    int   size;
    char  desPath[30];
    char  srcPath[30];
};

struct THREAD_LOAD_TASK
{
    Load_Task* pthread_load;
    int        iNode       ;
    char       file_name[30]; 
};

class BASE_OBJECT
{
public:
   BASE_OBJECT();
   virtual ~BASE_OBJECT();
   virtual bool excute(){ return true; };
   virtual bool getValue(const char* src, char* des1, char* des2);
    virtual bool analyse( const char* path);
    virtual bool getValueByFixChar(char* src, char des[][10], char sign);
    virtual bool copyFile(char * srcFile, char* desFile, char* openSign="a+");
    static bool getValueByFixChar2(char* src, char des[][30], char sign);
protected:
    static Login_Infor* pLoginInfor;
};

class FTP_CLIENT : public BASE_OBJECT
{
public:
    FTP_CLIENT();
    virtual ~FTP_CLIENT();
    virtual bool excute() { return true; };
    bool ftpInit();
    bool ftpCmd(int socketId, FTP_CMD strCmd, int paraInt=0, char* paraStr=NULL, PFV pf=NULL);
    bool ftpExcute(int socketId, char* sendCmd, char* returnCode, int waitTimes=WAIT_TIMES);
    bool ftpListen();
    bool ftpDownLoad(const char* pSavePath=NULL);
    bool ftpProcDownLoad(const Load_Task* pInfor);
    bool ftpUpLoad(const char* pUpPath);
    bool ftpSendSucc(int socketId, char* sendCmd);
    bool ftpRecvSucc(int socketId, char* returnCode=NULL, int waitTimes=WAIT_TIMES, bool IsNeedRecv=true);
    static void* ftpThreadFunc(void* p);
    static int  getOneFileSize(char* path, char* para);
    int  controlSock;
  int  listenSock;
  int  transferSock;
  int  tSize;
  char cmdBuffer[128];
  struct THREAD_LOAD_TASK pthread_loadtask;
private:
  struct sockaddr_in serverAddr;
  struct sockaddr_in clientAddr;
  struct Load_Task   *pLoadTaskInfor;
};

#endif