衡阳网站推广排名,潮南最新消息今晚,北京市城乡建设学校网站,基于html5的毕业设计论文注意#xff1a;本篇博客只是对上一篇博客功能的增加
1.创建配置文件(翻译)
Dict.txt apple: 苹果 banana: 香蕉 cat: 猫 dog: 狗 book: 书 pen: 笔 happy: 快乐的 sad: 悲伤的 run: 跑 jump: 跳 teacher: 老师 student: 学生 car: 汽车 bus: 公交车 love: 爱 hate: 恨 hell…注意本篇博客只是对上一篇博客功能的增加
1.创建配置文件(翻译)
Dict.txt apple: 苹果 banana: 香蕉 cat: 猫 dog: 狗 book: 书 pen: 笔 happy: 快乐的 sad: 悲伤的 run: 跑 jump: 跳 teacher: 老师 student: 学生 car: 汽车 bus: 公交车 love: 爱 hate: 恨 hello: 你好 goodbye: 再见 summer: 夏天 winter: 冬天 2.定义Dict类
#includestring
#includefstream
#includeunordered_map
#includelog.hppclass Dict
{//默认配置文件路径const std::string default_path ./Dict.txt;//默认分隔符const std::string sep : ;
private://将配置文件的内容加载到_dict中bool Load(){std::ifstream file(_dict_conf_file_path);if(!file.is_open()){LOG(FATAL,open %s error,_dict_conf_file_path);return false;}//按行读取文件std::string line;while(getline(file,line)){if(line ) continue;std::string word;size_t pos line.find(sep);word line.substr(0,pos);std::string han;han line.substr(possep.size());//将对应的key value插入到哈希桶中_dict.insert(make_pair(word,han));LOG(DEBUG,load info %s: %s\n,word,han);}file.close();return true;}
public:Dict(const std::string path ./Dict.txt):_dict_conf_file_path(path){Load();}std::string TranSlate(const std::string word){auto han _dict.find(word);if(han _dict.end()){return 这个单词未找到;}return han-second;}~Dict(){}
private://key value 结构的字典 单词 翻译std::unordered_mapstd::string,std::string _dict;//配置文件的目录std::string _dict_conf_file_path;
};
3.main函数中将翻译模块和网络模块分开 //翻译模块Dict dict;//网络模块//智能指针创建UdpServerstd::unique_ptrUdpServer usvr std::make_uniqueUdpServer( port,std::bind(Dict::TranSlate,dict,std::placeholders::_1) ); //启动UdpServervoid Stat(){_running true;//服务器一般都是死循环while(true){sockaddr_in peer;//recvfrom的最后一个参数类型是socklen_tsocklen_t len sizeof(peer);char buffer[1024];//从接收缓存区中读取数据报int n recvfrom(_sockfd,buffer,sizeof(buffer)-1,0,(struct sockaddr*)peer,len);//读取到数据才做处理否则什么都不做if(n 0){buffer[n] 0;//打印是哪个客户端发来的消息InetAddr addr(peer);LOG(INFO,message form[%s:%d]: %s\n,addr.GetIp().c_str(),addr.Get_Port(),buffer);//将翻译发送给对方std::string response _func(buffer);sendto(_sockfd,response.c_str(),response.size(),0,(sockaddr*)peer,len);}}_running false;}
文章转载自: http://www.morning.zglrl.cn.gov.cn.zglrl.cn http://www.morning.wfjrl.cn.gov.cn.wfjrl.cn http://www.morning.jpwmk.cn.gov.cn.jpwmk.cn http://www.morning.srkzd.cn.gov.cn.srkzd.cn http://www.morning.muzishu.com.gov.cn.muzishu.com http://www.morning.ljllt.cn.gov.cn.ljllt.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.rzdpd.cn.gov.cn.rzdpd.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.jfjbl.cn.gov.cn.jfjbl.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.lokext.com.gov.cn.lokext.com http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.bhjyh.cn.gov.cn.bhjyh.cn http://www.morning.jhqcr.cn.gov.cn.jhqcr.cn http://www.morning.bfnbn.cn.gov.cn.bfnbn.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.xinxianzhi005.com.gov.cn.xinxianzhi005.com http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.gqcsd.cn.gov.cn.gqcsd.cn http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.brscd.cn.gov.cn.brscd.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.jjhrj.cn.gov.cn.jjhrj.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.zdnrb.cn.gov.cn.zdnrb.cn http://www.morning.gcszn.cn.gov.cn.gcszn.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.rrbhy.cn.gov.cn.rrbhy.cn http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.qfrsm.cn.gov.cn.qfrsm.cn http://www.morning.bnfjh.cn.gov.cn.bnfjh.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.fssjw.cn.gov.cn.fssjw.cn http://www.morning.djpgc.cn.gov.cn.djpgc.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.tbjtm.cn.gov.cn.tbjtm.cn http://www.morning.mqffm.cn.gov.cn.mqffm.cn http://www.morning.sqgqh.cn.gov.cn.sqgqh.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.dzqyn.cn.gov.cn.dzqyn.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.tnhg.cn.gov.cn.tnhg.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.qxwwg.cn.gov.cn.qxwwg.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.psxfg.cn.gov.cn.psxfg.cn http://www.morning.ymtbr.cn.gov.cn.ymtbr.cn http://www.morning.gltmz.cn.gov.cn.gltmz.cn http://www.morning.wkwds.cn.gov.cn.wkwds.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.wrtbx.cn.gov.cn.wrtbx.cn http://www.morning.jydhl.cn.gov.cn.jydhl.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.qqpg.cn.gov.cn.qqpg.cn http://www.morning.xmjzn.cn.gov.cn.xmjzn.cn http://www.morning.ddzqx.cn.gov.cn.ddzqx.cn http://www.morning.tnhqr.cn.gov.cn.tnhqr.cn http://www.morning.xlxmy.cn.gov.cn.xlxmy.cn