linux socket编程之udp_dict_serve服务端--引入配置文件

注意:本篇博客只是对上一篇博客功能的增加

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类

#include<string>
#include<fstream>
#include<unordered_map>
#include"log.hpp"

class 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(pos+sep.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_map<std::string,std::string> _dict;
    //配置文件的目录
    std::string _dict_conf_file_path;
};

3.main函数中将翻译模块和网络模块分开

    //翻译模块
    Dict dict;
    //网络模块
    //智能指针创建UdpServer
    std::unique_ptr<UdpServer> usvr = std::make_unique<UdpServer>( port,
             std::bind(&Dict::TranSlate,&dict,std::placeholders::_1) );

    //启动UdpServer
    void Stat()
    {
        _running = true;
        //服务器一般都是死循环
        while(true)
        {
            sockaddr_in peer;
            //recvfrom的最后一个参数类型是socklen_t
            socklen_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;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值