Linux网络编程:UDP 的DictServer
1、数据字典字典那我们是不是需要一个txt文件里面包含了我们用来测试字典的数据我们创建一个data.txt文件从中添加一系列格式为英文中文的测试用例随便写几个测试用例就行了。2、字典类与头文件我们先写出一个字典类把这个类定义到我们的字典头文件中去。随后用两个全局变量来记录data.txt文件的路径与文件名。因为我们要使用字典必然会读取文件里面的数据这里就涉及到了文件操作。一个字典的功能必须要有初始化与翻译所以我们可以先把这两个接口给定义一下代码语言javascriptAI代码解释#pragma once #ifndef __DICTIONARY_HPP__ #define __DICTIONARY_HPP__ #include unordered_map #include string const std::string dataname data.txt; // 定义好我们存储字典数据的文件名 const std::string path ./; // 定义好我们存储字典数据的路径 class dictionary { public: dictionary() { } ~dictionary() {} bool LoadDictionary() // 我们需要一个函数来加载字典数据初始化我们的字典对象填充字典数据到哈希表里 { } std::string Translate(const std::string word) // 我们之后用这个函数来实现一个翻译的功能 { } private: std::unordered_mapstd::string, std::string dict; }; #endif我们还可以给我们的类增加一下路径和名字成员变量方便记录管理与使用。代码语言javascriptAI代码解释std::string _filename; std::string _path;// 我们可以把文件名和路径都存储在字典对象里在构造一个字典对象时就在构造函数中通过确实的上面的全局变量值对我们成员变量进行初始化代码语言javascriptAI代码解释dictionary(const std::string filenamedataname,std::string pathdefaultpath) :_filename(filename), _path(path) { }接下来我们来实现一下加载字典的操作。读取一个txt的数据必然要涉及到文件操作。我们这里使用C的文件操作流。所以可以在LoadDictionary中先新建一个输入流但是这里打开文件涉及到这个文件的路径所以我们就需要先合成一下这个文件的路径信息随后进行ifstream的创建。如果打开失败就打印错误提示信息代码语言javascriptAI代码解释std::string filepath_path_filename; // 拼接文件路径和文件名 std::ifstream fd(filepath.c_str()); // 打开文件,默认以读取方式打开 if(!fd.is_open()) { LOG(LogLevel::ERROR)Failed to open dictionary file: filepath; return false; // 如果文件打开失败返回false }如果打开成功我们就需要读取数据了。怎么读取呢我们通过getline读取到一行string格式为apple: 苹果。我们这里使用哈希表存储这个格式怎么可以存储呢所以我们需要对其进行切割如何切割我们定义一个切割接口并传入以下函数参数string类型的line值string*的word与value值用来接收被分割后的字符串这两个都是输出型参数。最后一个sep表示用什么来切割。代码语言javascriptAI代码解释bool Split(std::string line,std::string *word,std::string *value,const std::string sep) { int pos line.find(sep);// 找到的位置 if(posstd::string::npos)//没找到 { return false; } *wordline.substr(0,pos);//pos找到之后所指向的位置就是:的位置substr是一个左闭右开区间 *value line.substr(possep.size());// 从possep.size()开始截取直到字符串结束 if(value-empty()||word-empty()) { return false; // 如果截取的值为空返回false } return true; }完成切割后就把该对pair加入到我们的字典中去代码语言javascriptAI代码解释bool LoadDictionary() // 我们需要一个函数来加载字典数据初始化我们的字典对象填充字典数据到哈希表里 { std::string filepath_path_filename; // 拼接文件路径和文件名 std::ifstream fd(filepath.c_str()); // 打开文件,默认以读取方式打开 if(!fd.is_open()) { LOG(LogLevel::ERROR)Failed to open dictionary file: filepath; return false; // 如果文件打开失败返回false } std::string line; while(getline(fd,line)) { std::string word,value; if(Split(line,word,value,: )) // 假设我们用:来分隔单词和释义 { dict[word]value; // 将单词和释义存入哈希表 } } }那么接下来就是完成我们的翻译工作了。首先这个翻译功能我们想要实现的效果就是传递一个word值调用这个接口进行翻译。我们在翻译里面查找哈希表如果找到该word就返回这个word所对应的value代码语言javascriptAI代码解释std::string Translate(const std::string word) // 我们之后用这个函数来实现一个翻译的功能 { auto itdict.find(word); if(it ! dict.end()) // 如果找到了这个单词 { return it-second; // 返回对应的释义 } else { LOG(LogLevel::WARN)Word not found: word; return Not found; // 如果没找到返回一个默认值 } }至此我们的一个字典类就大功告成了代码语言javascriptAI代码解释#pragma once #ifndef __DICTIONARY_HPP__ #define __DICTIONARY_HPP__ #include unordered_map #include string #include iostream #includestdio.h #includelog.hpp using namespace LogModule; const std::string dataname data.txt; // 定义好我们存储字典数据的文件名 const std::string defaultpath ./; // 定义好我们存储字典数据的路径 class dictionary { public: dictionary(const std::string filenamedataname,std::string pathdefaultpath) :_filename(filename), _path(path) { } ~dictionary() {} bool Split(std::string line,std::string *word,std::string *value,const std::string sep) { int pos line.find(sep);// 找到的位置 if(posstd::string::npos)//没找到 { return false; } *wordline.substr(0,pos);//pos找到之后所指向的位置就是:的位置substr是一个左闭右开区间 *value line.substr(possep.size());// 从possep.size()开始截取直到字符串结束 if(value-empty()||word-empty()) { return false; // 如果截取的值为空返回false } return true; } bool LoadDictionary() // 我们需要一个函数来加载字典数据初始化我们的字典对象填充字典数据到哈希表里 { std::string filepath_path_filename; // 拼接文件路径和文件名 std::ifstream fd(filepath.c_str()); // 打开文件,默认以读取方式打开 if(!fd.is_open()) { LOG(LogLevel::ERROR)Failed to open dictionary file: filepath; return false; // 如果文件打开失败返回false } std::string line; while(getline(fd,line)) { std::string word,value; if(Split(line,word,value,: )) // 假设我们用:来分隔单词和释义 { dict[word]value; // 将单词和释义存入哈希表 } } } std::string Translate(const std::string word) // 我们之后用这个函数来实现一个翻译的功能 { auto itdict.find(word); if(it ! dict.end()) // 如果找到了这个单词 { return it-second; // 返回对应的释义 } else { LOG(LogLevel::WARN)Word not found: word; return Not found; // 如果没找到返回一个默认值 } } private: std::unordered_mapstd::string, std::string dict; std::string _filename; std::string _path;// 我们可以把文件名和路径都存储在字典对象里 }; #endif二、使用字典类那我们应该如何使用我们写好的字典类呢这里就要修改一下我们的服务端了我们需要修改一下我们服务端启动的参数。比如我们可以先使用智能指针创建一个字典对象随后调用这个对象中的加载数据的函数。数据加载完毕之后我们尝试回调函数的方式将我们的Translate函数通过回调传递进去代码语言javascriptAI代码解释// 创建一个由智能指针所管理的字典对象 std::shared_ptrdictionary dict_ptr std::make_shareddictionary(); dict_ptr-LoadDictionary(); // 加载字典数据 // 我们先创建一个服务器对象并用智能指针管理它 std::unique_ptrUdpServer svr_ptr std::make_uniqueUdpServer(std::string(127.0.0.1), 8080, [dict_ptr](const std::string word) - std::string { return dict_ptr-Translate(word); }); // 回调函数的方式传递我们的翻译函数三、UdpServer.hpp的回调实现那么我们应该如何修改我们的头文件呢首先我们需要将一个lambda表达式传进去帮助这个服务端对象初始化所以我们的服务端的初始化函数就必须新增对应的初始化。由于传进去的是一个返回值是string参数是const string的一个函数类型。我们可以在头文件中使用function帮助我们代码语言javascriptAI代码解释using find_t std::functionstd::string(const std::string );至此我们就能使用find_t类型代替我们传进来的类类型。由于新增了初始化所以我们的的服务端类需要新增一个类成员变量代表着传进来的翻译函数接口并在构造函数中初始化我们这里也给其设置一个缺省函数否则就要改变我们的参数顺序非全缺省缺省需要放在右边这样一来我们的_findword就代表着这个函数。我们在start中会受到客户端的消息我们拿到消息后进入翻译函数并返回翻译信息代码语言javascriptAI代码解释void Start() { is_running true; while (is_running) { char buffer[1024]; struct sockaddr_in peer; // 输出型参数 socklen_t len sizeof(peer); // 也是一个输出型参数 ssize_t n ::recvfrom(_sockfd, buffer, sizeof(buffer), 0, (struct sockaddr *)peer, len); if (n 0) { InetAddr temp(peer); // 通过上面的peer来进行初始化这样以来我们就能获取到相关ip地址与端口并打印 buffer[n] \0; // 确保字符串以null结尾 // LOG(LogLevel::INFO) client ip: temp.GetIp() , port: temp.GetPort() // client say: buffer; std::string echo_str Translate: ; // 我们要给客户端回显一条消息 echo_str _findword(std::string(buffer)); // 调用传入的函数来获取翻译结果 // 发送回显消息 ssize_t m ::sendto(_sockfd, echo_str.c_str(), echo_str.size(), 0, (struct sockaddr *)peer, len); if (m 0) { LOG(LogLevel::ERROR) sendto error: strerror(errno); } } } }这样一来一个简单的字典程序就大功告成了。我们可以运行一下代码总结今天的内容比较简单这是我在为明天的聊天室做一个过渡作用。希望大家那个熟练使用我们这里的回调方法明天会经常用到。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2430145.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!