当前位置: 首页 > news >正文

网站需求建设书济南网站优化

网站需求建设书,济南网站优化,营销网站的建设流程,深圳营销型网站建设 龙华信科文章目录 前言一. 序列化和反序列化1.自己实现2. JSON 二. 初识协议结束语 前言 本系列文章是计算机网络学习的笔记,欢迎大佬们阅读,纠错,分享相关知识。希望可以与你共同进步。 本篇博文正式开始应用层的学习,首先讲解应用层的…

文章目录

  • 前言
  • 一. 序列化和反序列化
    • 1.自己实现
    • 2. JSON
  • 二. 初识协议
  • 结束语

前言

本系列文章是计算机网络学习的笔记,欢迎大佬们阅读,纠错,分享相关知识。希望可以与你共同进步。

本篇博文正式开始应用层的学习,首先讲解应用层的序列化和反序列化,还有了解简单的应用层传输协议

一. 序列化和反序列化

在前篇TCP和UDP服务器编写时,业务只是简单的echo客户端发送的数据,但实际生活中,要传输的数据往往复杂的多。使用结构体或类可以保存更多数据,但传输过程中,可能会遇到网络通信两端的操作系统不同,结构体/类的大小不同,内存对齐策略不一致等问题。所以网络传输十分不建议传输结构体或类
这时,序列化和反序列化诞生了。

序列化通俗来说,就是将结构体/类转化为字符串;而反序列化就是将字符串转化为结构体

序列化最重要的作用:在传递和保存对象时,保证对象的完整性和可传递性等问题。对象转换为有序字节流,以便在网络上传输或者保存在本地文件中。

反序列化最重要的作用:根据字节流中保存的对象状态及描述信息,通过反序列化重建对象

核心作用就是对象状态的保存和重建。(整个过程核心点就是字节流所保存的对象状态及描述信息

1.自己实现

案例

假如我们现在要实现一个网络版本的计算器
我们把客户端发送的数据称为需求(Request),服务器返回的数据叫做响应(Responce)
此案例的需求有三个变量:数字x,数字y,操作数op
响应有两个变量:结果result,结果码code
需求和响应都需要有序列化反序列化两个功能函数
代码如下:

使用分割符方便Request提取变量,分隔符——空格
1+1 =>(添加分隔符) 1 + 1


#define SEP " " //分隔符
#define SEP_LEN strlen(SEP)
// 请求
class Request
{
public:Request() {}// 序列化 结构体=>字符串bool Serialize(std::string *outStr){*outStr = "";std::string x_string = std::to_string(_x);std::string y_string = std::to_string(_y);//添加分隔符*outStr = x_string + SEP + _op + SEP + y_string;return true;}// 反序列化 字符串=>结构体bool Deserialize(const std::string &str){std::vector<std::string> result;Util::StringSplit(str, SEP, &result);//必须提取出三个变量if (result.size() != 3)return false;_x = atoi(result[0]);_op = result[1][0];_y = atoi(result[2]);return true;}
public:int _x;int _y;char _op;
};// 响应
class Responce
{
public:Responce() : _result(0), _code(0){}// 序列化 结构体->字符串bool Serialize(std::string *outStr){*outStr = "";std::string result_string = std::to_string(_result);std::string code_string = std::to_string(_code);*outStr = result_string + SEP + code_string;return true;}//反序列化 字符串->结构体bool Deserialize(const std::string &str){std::vector<std::string> result;Util::StringSplit(str, SEP, &result);if (result.size() != 2)return false;_result = atoi(result[0]);_code = atoi(result[1]);return true;}
public:int _result;int _code;
};

2. JSON

序列化一般我们不自己操作,可以使用别的封装好的序列化,比如:JSON,ProtocolBuffer,FlatBuffer,DIMBIN

本篇文章介绍json的使用
JSON 的语法规则总结起来有:

  • 数组(Array)用方括号(“[]”)表示。
  • 对象(0bject)用大括号(“{}”)表示。
  • 名称/值对(name/value)组合成数组和对象。
  • 名称(name)置于双引号中,值(value)有字符串、数值、布尔值、null、对象和数组。
  • 并列的数据之间用逗号(“,”)分隔

序列化后可视效果也较好,比如:

{"x":10,"y":22,"op":*
}

代码:

// 请求
class Request
{
public:Request() {}// 结构体=>字符串bool Serialize(std::string *outStr){// Value,一种万能对象,接收任意的kv类型Json::Value root;root["x"] = _x;root["y"] = _y;root["op"] = _op;// 序列化Json::StyledWriter writer;*outStr = writer.write(root);return true;}// 字符串=>结构体bool Deserialize(const std::string &str){Json::Value root;// 反序列化Json::Reader reader;reader.parse(str, root);//提取变量_x = root["x"].asInt();_y = root["y"].asInt();_op = root["op"].asInt();return true;}
public:int _x;int _y;char _op;
};
// 响应
class Responce
{
public:Responce() : _result(0), _code(0){}// 结构体->字符串bool Serialize(std::string *outStr){Json::Value root;root["result"] = _result;root["code"] = _code;// 序列化Json::StyledWriter writer;*outStr = writer.write(root);return true;}bool Deserialize(const std::string &str){Json::Value root;//反序列化Json::Reader reader;reader.parse(str, root);//提取变量_result = root["result"].asInt();_code = root["code"].asInt();return true;}
public:int _result;int _code;
};

二. 初识协议

在网络通信中,客户端,服务器收发数据其实是如下这样的

在这里插入图片描述

我们调用的recv,send等接口,只是将我们定义的缓冲区的数据拷贝到TCP的缓冲区,或者将数据从TCP缓冲区拷贝到上层
TCP是面向字节流的,可以认为,数据是一个字节一个字节传输的
如果,客户端发送一个hello,recv接口会将发送缓冲区的数据一次性拷贝到上层,但可能此时通过网络传输,只传送了hel,服务器的接受缓冲区只有hel,此时recv并没有读取到完整报文,并且我们不知道什么时候读到了完整报文
协议就是为了解决这类问题而诞生的

基于本次网络计算机的案列,简单设计的协议比如,添加长度和\r\n报头
比如:“1 + 1” =>“5"”\r\n"“1 + 1"”\r\n"

解析:通过找到第一个\r\n,获取有效载荷(1 + 1)的长度,再根据长度提取有效载荷

代码如下:

#define HEADER_SEP "\r\n" // 报头分隔符
#define HEADER_SEP_LEN strlen(HEADER_SEP)
// 读取数据,并尝试提取一个完整报文
// 完整报文:"有效载荷长度""\r\n""有效载荷""\r\n"
//inbuffer保存所有读取的数据,package是一个完整报文,需要输出
int ReadPackage(int sock, std::string &inbuffer, std::string *package)
{// 读数据char buffer[1024];int n = recv(sock, buffer, sizeof(buffer) - 1, 0);if (n > 0)buffer[n] = '\0';else if (n == 0)//写端关闭return -1;else if (n < 0)//读取异常return -2;//将本次读取的数据保存inbuffer += buffer;//开始查找报头分隔符size_t start = 0;auto pos = inbuffer.find(HEADER_SEP, start);if (pos == std::string::npos)return 0; //第一个分隔符都没有,不是完整报文//提取长度std::string lenStr = inbuffer.substr(0, pos);int len = Util::toInt(lenStr); // 有效载荷的长度// 完整报文的长度int targetPackageLen = lenStr.size() + len + 2 * HEADER_SEP_LEN;if (inbuffer.size() < targetPackageLen)return 0; //长度不足完整报文// 提取一个完整报文,并输出*package = inbuffer.substr(0, targetPackageLen);inbuffer.erase(0, targetPackageLen);return len;
}

结束语

本篇博客到此结束,感谢看到此处。
欢迎大家纠错和补充
如果觉得本篇文章对你有所帮助的话,不妨点个赞支持一下博主,拜托啦,这对我真的很重要。
在这里插入图片描述


文章转载自:
http://wigwam.qwfL.cn
http://perchlorate.qwfL.cn
http://qairwan.qwfL.cn
http://caprate.qwfL.cn
http://snazzy.qwfL.cn
http://antiemetic.qwfL.cn
http://isochronize.qwfL.cn
http://procedure.qwfL.cn
http://intraparty.qwfL.cn
http://piemonte.qwfL.cn
http://sirree.qwfL.cn
http://untouchability.qwfL.cn
http://pettifogger.qwfL.cn
http://stud.qwfL.cn
http://kinsfolk.qwfL.cn
http://umbrose.qwfL.cn
http://rattler.qwfL.cn
http://aroynt.qwfL.cn
http://sciolto.qwfL.cn
http://queenlike.qwfL.cn
http://dandriff.qwfL.cn
http://porker.qwfL.cn
http://inappetent.qwfL.cn
http://spire.qwfL.cn
http://mitsein.qwfL.cn
http://doghouse.qwfL.cn
http://prolongate.qwfL.cn
http://choriamb.qwfL.cn
http://mitered.qwfL.cn
http://anopsia.qwfL.cn
http://ixodid.qwfL.cn
http://rotor.qwfL.cn
http://eponym.qwfL.cn
http://yucatec.qwfL.cn
http://nationhood.qwfL.cn
http://beaucoup.qwfL.cn
http://sulphazin.qwfL.cn
http://recursion.qwfL.cn
http://vichy.qwfL.cn
http://hundredweight.qwfL.cn
http://sice.qwfL.cn
http://recreative.qwfL.cn
http://largely.qwfL.cn
http://philter.qwfL.cn
http://forworn.qwfL.cn
http://postrorse.qwfL.cn
http://racquet.qwfL.cn
http://lichenology.qwfL.cn
http://examples.qwfL.cn
http://whyfor.qwfL.cn
http://cytotrophy.qwfL.cn
http://maryolatrous.qwfL.cn
http://legitimize.qwfL.cn
http://troophorse.qwfL.cn
http://fukien.qwfL.cn
http://goluptious.qwfL.cn
http://goosander.qwfL.cn
http://dungeon.qwfL.cn
http://bluff.qwfL.cn
http://misdata.qwfL.cn
http://agonise.qwfL.cn
http://osar.qwfL.cn
http://rocklike.qwfL.cn
http://wherever.qwfL.cn
http://commision.qwfL.cn
http://imprecate.qwfL.cn
http://preceptory.qwfL.cn
http://dependant.qwfL.cn
http://confident.qwfL.cn
http://hanseatic.qwfL.cn
http://devoutly.qwfL.cn
http://epitomist.qwfL.cn
http://frith.qwfL.cn
http://offering.qwfL.cn
http://graceless.qwfL.cn
http://emanatorium.qwfL.cn
http://astrolithology.qwfL.cn
http://reit.qwfL.cn
http://digitate.qwfL.cn
http://gatepost.qwfL.cn
http://muscardine.qwfL.cn
http://sporeling.qwfL.cn
http://wearer.qwfL.cn
http://crankish.qwfL.cn
http://gayal.qwfL.cn
http://mississippi.qwfL.cn
http://jildi.qwfL.cn
http://towerless.qwfL.cn
http://art.qwfL.cn
http://lobbyism.qwfL.cn
http://kawasaki.qwfL.cn
http://squinch.qwfL.cn
http://autopia.qwfL.cn
http://egp.qwfL.cn
http://typhus.qwfL.cn
http://pretubercular.qwfL.cn
http://disulfuram.qwfL.cn
http://gelidity.qwfL.cn
http://sixain.qwfL.cn
http://vasa.qwfL.cn
http://www.15wanjia.com/news/85046.html

相关文章:

  • 做网站 华普花园百度旗下的所有产品
  • 域名备案名称搜索引擎优化培训中心
  • 网站制作出租鞍山seo公司
  • 能领免做卡的网站搜索引擎营销分类
  • 东莞高端建站公司域名注册好了怎么弄网站
  • 网站建设项目预算今日新闻快讯
  • php 微网站开发seo成功的案例和分析
  • wordpress你访问的网站不存在制作网页的网站
  • 广州北京网站建设公司哪家好淘宝关键词优化软件
  • 网站开发ios环球网疫情最新动态
  • 建筑模板规格尺寸及价格整站seo优化公司
  • 关于网站得精神文明建设优化大师官方网站
  • 怎么注册微网站肇庆网站制作软件
  • 做驾考学时在哪个网站视频号最新动作
  • wordpress离线字体优化网站关键词
  • 做阿里巴巴网站费用吗如何做google推广
  • 手机优化电池充电要开吗网站seo推广
  • 海宏集团网站建设上海网站制作
  • 益阳网站开发公司社群营销
  • 网站备案 公司注销吗前端seo是什么意思
  • 网络工作室适合做什么性价比高seo的排名优化
  • 怎样做网站漂浮百度收录情况
  • 低价网站空间免费seo诊断
  • 网站做百度排名b站广告投放平台入口
  • 学做蛋糕有哪些网站怀化seo推广
  • 主题资源网站制作平台网络销售工资一般多少
  • 网站地图的形式新站seo优化快速上排名
  • 汕头各类免费建站百度认证有什么用
  • 建个什么网站好百度指数app
  • 哪有可以专门做外包项目的网站电商平台引流推广