当前位置: 首页>>代码示例>>C++>>正文


C++ Host::hasService方法代码示例

本文整理汇总了C++中Host::hasService方法的典型用法代码示例。如果您正苦于以下问题:C++ Host::hasService方法的具体用法?C++ Host::hasService怎么用?C++ Host::hasService使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Host的用法示例。


在下文中一共展示了Host::hasService方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: ShellService

FTPService::FTPService(unsigned int version): ShellService(Cache::queryCache("FTP"), 21, version) {
    Shell* shell = getShell();
    shell->add("help", [this] (vector<string> args) {
        cout << "list: list all files" << endl;
        cout << "upload [file] [host]: upload file to host" << endl;
        cout << "delete [file]: delete a file" << endl;
        cout << "cat [file]: display file contents" << endl;
    }, false);
    shell->add("list", [this] (vector<string> args) {
        // list all files
        cout << setw(width) << "Filename ";
        cout << "Size" << endl;
        for(File *file: files) {
            cout << left << setw(width) << file->name->get();
            cout << file->contents->get().length() << "b" << endl;
        }
    }, true);
    shell->add("upload", [this] (vector<string> args) {
        // upload a local file to a remote FTP server
        if(args.size() < 3) {
            cout << "Please specify file name and remote host." << endl;
        }
        
        string filename = args[1];
        string ip = args[2];
        
        // check file
        File *file = nullptr;
        for(File *f: files) {
            if(f->name->get() == filename) {
                file = new File{f->name, f->contents};
                break;
            }
        }
        
        if(!file) {
            cout << "File not found." << endl;
            return;
        }
        
        // check host
        if(!localhost->ping(ip)) {
            cout << "Destination host unreachable." << endl;
            delete file;
            return;
        }
        
        // find host
        Host *remote = localhost->resolve(ip);
        if(!remote->hasService(21)) {
            cout << "Connection refused." << endl;
            delete file;
            return;
        }
        
        // contact server
        FTPService *ftp = static_cast<FTPService*>(remote->getService(21));
        ftp->upload(file);
        
        cout << "Upload complete." << endl;
    }, true);
    shell->add("delete", [this] (vector<string> args) {
        if(args.size() < 2) {
            cout << "Please specify a file name." << endl;
        }
        
        // delete file
        string name = args[1];
        for(auto itr = files.begin(); itr != files.end(); itr++) {
            File *file = *itr;
            if(file->name->get() == name) {
                files.erase(itr);
                return;
            }
        }
        
        cout << "File not found." << endl;
    }, true);
    shell->add("cat", [this] (vector<string> args) {
        if(args.size() < 2) {
            cout << "Please specify a file name." << endl;
            return;
        }
        
        string name = args[1];
        for(File *file: files) {
            if(file->name->get() == name) {
                cout << file->contents << endl;
                return;
            }
        }
        
        cout << "File not found." << endl;
    }, true);
}
开发者ID:peck94,项目名称:hacker,代码行数:95,代码来源:FTPService.cpp


注:本文中的Host::hasService方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。