LINUX网络编程:应用层和协议定制

目录

1.协议定制

2.序列化和反序列化

​编辑

3.tcp为什么是全双工

4.Tcp保证接收数据的完整性


1.协议定制

定制协议就是通信双方都遵守的协定 加上 符合通信和业务处理的结构化数据,就是struct或class。

例:佩奇使用微信向乔治发送了【你好】,这条消息不仅仅包含【你好】本身,还可能有这条消息的时间或者是这条消息来自哪个人。

class message
{
    std::string mesg;//消息内容
    std::string who;//谁发的
    time_t  time;//发送时间
};

message就是符合通信和业务处理的结构化数据

双方都遵守的协定如何理解呢?

假定我们现在要处理计算业务,resquset是业务处理的结构化数据。

我们规定计算的格式 _x _oper _y,_oper可以是+ - * / %。这个就是需要双方都遵守的协定。

class request
{

private:
    int _x;
    int _y;
    char _oper;//运算符
};

2.序列化和反序列化

序列化:将结构化的数据转化为字符串

反序列化:将字符串转化为结构化数据

我想把request这个请求发送到网络当中,第一件事情就是将resquest序列化。

为什么要序列化呢??

1.如果直接发结构化的数据是有跨平台的问题的,因为不同平台下内存对齐的规则是不同。

2.序列化之后传输的效率会更高,因为结构化的数据会有内存对齐,有内存对齐就会有空间的浪费。

3.tcp为什么是全双工

全双工:双方可以同时发送数据

半双工:同时只有一方可以发送数据

因为在tcp有两个缓冲区,一个用来接收数据,一个用来发送数据。

 网络通信本质也就是数据的拷贝,将自己发送缓冲区的内容,拷贝到对方的接收缓冲区中。 

4.Tcp保证接收数据的完整性

tcp是面向字节流的,在接收时,可能收到的不是一个完整的数据,所以在应用层,我们必须保证数据时完整的。

通常会在序列化之后的字符串添加一个报头,发送字符串的长度,这样就能判断当前的数据是否完整。

5.简单计算协议的实现

#pragma once
#include <string>
#include <jsoncpp/json/json.h>

namespace protocol_ns
{
    const std::string sep = "\r\n";

    std::string encode(const std::string &jsonstr) // 给json串添加报头
    {
        int jsonlen = jsonstr.size();
        std::string protocol_str = std::to_string(jsonlen);
        protocol_str += sep;
        protocol_str += jsonstr;
        protocol_str += sep;
        return protocol_str;
    }
    // len\r\n{      }\r\n
    std::string decode(std::string &inbuffer) // 解析报头,返回一个完整的json串
    {
        auto pos = inbuffer.find(sep);
        if (pos == std::string::npos) // inbuffer 中没有sep
        {
            return std::string();
        }
        std::string lenstr = inbuffer.substr(0, pos);
        if (lenstr.empty())
        {
            return std::string();
        }
        int josnlen = std::stoi(lenstr);
        int total = josnlen + sep.size() * 2 + lenstr.size();

        if (inbuffer.size() < total) // 说明inbuffer里有一个完整的报文
        {
            return std::string();
        }
        std::string josnstr = inbuffer.substr(pos + sep.size(), josnlen);
        ;
        inbuffer.erase(0, total);
        return josnstr;
    }

    class resquest
    {
    public:
        resquest(int x, int y, char oper)
            : _x(x), _y(y), _oper(oper)
        {
        }

        resquest()
        {

        }
        bool serialize(std::string &out) // 将结构化的数据转化为json串
        {
            Json::Value root;
            root["x"] = _x;
            root["y"] = _y;
            root["oper"] = _oper;
            Json::FastWriter writer;
            out = writer.write(root);
            return true;
        }

        bool deserialize(std::string &in) // 将josn串转化为结构化数据
        {
            Json::Value root;
            Json::Reader reader;
            bool res = reader.parse(in,root);
            if(!res)
            {
                return false;
            }
            _x = root["x"].asInt();
            _y = root["y"].asInt();
            _oper = root["oper"].asInt();
            return true;
        }
        int _x;
        int _y;
        char _oper;
    };

    class respond
    {
    public:
        respond(int res, int code)
            : _res(res), _code(code)
        {
        }
        
        respond()
        {

        }
        bool serialize(std::string &out)
        {
            Json::Value root;
            root["res"] = _res;
            root["code"] = _code;
            Json::FastWriter writer;
            out = writer.write(root);
            return true;
        }

        bool deserialize(std::string &in)
        {
            Json::Value root;
            Json::Reader reader;
            bool res = reader.parse(in,root);
            if(!res)
            {
                return false;
            }
            _res = root["res"].asInt();
            _code = root["code"].asInt();
            return true;
        }

    
        int _res;
        int _code; // 0结果可信,1除零错误,2其他错误
    };

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值