一个简单的文件共享工程 -- FileClient

FileClient文件夹中文件:

FileClient.h:

#ifndef __FILE_CLIENT_H__
#define __FILE_CLIENT_H__

#include <map>
#include <string>
using std::map;
using std::string;

#include "../TMClient/TMClient.h"

class FileClient : public TMClient
{
public:
    FileClient(const char * host, const char * serv);
    ~FileClient();

protected:
    virtual bool handlecmd(char * data);

protected:
    bool send(const char * data, int n);
    bool recv(char * buff, int n);
    void showusage();

private:
    bool handle_ls_res();
    bool handle_cd_res();
    bool handle_mkdir_res();
    bool handle_touch_res();
    bool handle_rm_res();
    bool handle_download_res();
    bool handle_upload_res(char * data);

private:
    bool handle_normal_res();
    bool upsend(unsigned char state, char * buff, unsigned short uslen);
    int upfile(char * filename, char * buff, unsigned short uslen);
    int updir(char * pathname, char * buff, unsigned short uslen);

protected:
    map<string, unsigned char>  m_cmd;
};

#endif
FileClient.cpp:

#include <errno.h>
#include <fcntl.h>
#include <dirent.h>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <utility>
using std::pair;

#include "../ByteStream/ByteStream.h"
#include "../Head/Command.h"
#include "FileClient.h"

FileClient::FileClient(const char * host, const char * serv)
 : TMClient(host, serv)
{
    m_cmd.insert(pair<string, unsigned char>("ls", ls));
    m_cmd.insert(pair<string, unsigned char>("cd", cd));
    m_cmd.insert(pair<string, unsigned char>("mkdir", mk));
    m_cmd.insert(pair<string, unsigned char>("touch", touch));
    m_cmd.insert(pair<string, unsigned char>("rm", rm));
    m_cmd.insert(pair<string, unsigned char>("download", download));
    m_cmd.insert(pair<string, unsigned char>("upload", upload));
}

FileClient::~FileClient()
{
    
}

bool FileClient::handlecmd(char * data)
{
    unsigned short uslen = strlen(data);

    int fws;
    for (fws = 0; fws < uslen; ++fws) {
        if (' ' != data[fws]) {
            break;
        }
    }
    if (fws >= uslen) {
        showusage();
        return(true);
    }
    
    int fwe;
    for (fwe = fws + 1; fwe < uslen; ++fwe) {
        if (' ' == data[fwe]) {
            break;
        }
    }
    data[fwe] = '\0';

    int lws;
    for (lws = fwe + 1; lws < uslen; ++lws) {
        if (' ' != data[lws]) {
            break;
        }
    }
    
    int lwe;
    for (lwe = uslen - 1; lwe > lws; --lwe) {
        if (' ' != data[lwe]) {
            break;
        }
    }
    data[lwe + 1] = '\0';
    
    map<string, unsigned char>::iterator iter = m_cmd.find(data + fws);
    if (m_cmd.end() == iter) {
        showusage();
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值