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

消防设备网站建设私有云可以建设网站

消防设备网站建设,私有云可以建设网站,做盗文网站,专业做球赛旅游的网站文章目录 应用层初识TCP协议通讯流程定制协议再谈协议网络版本计算器Protocal.hppCalServerCalClient Json的安装 应用层 我们程序员写的一个个解决我们实际问题, 满足我们日常需求的网络程序, 都是在应用层 初识TCP协议通讯流程 建立链接和断开链接 基于TCP协议#xff0c… 文章目录 应用层初识TCP协议通讯流程定制协议再谈协议网络版本计算器Protocal.hppCalServerCalClient Json的安装 应用层 我们程序员写的一个个解决我们实际问题, 满足我们日常需求的网络程序, 都是在应用层 初识TCP协议通讯流程 建立链接和断开链接 基于TCP协议我们需要知道写代码时对应的接口大概在TCP通讯的过程中属于什么样的时间点角色在TCP协议时详谈。三次握手四次挥手 listen状态准备好了可以进行链接accept:获取链接不是创造链接链接已经在底层创建好了在应用层调用accept把链接拿上来 connect:1.发起链接请求2.绑定套接字建立链接在底层向服务端建立链接请求在TCP中采用链接的方案是三次握手的方案connect会发起三次握手发起链接请求和真正的建立链接是两码事建立链接由双方OS自动完成的为什么自动完成网络分层中下三层是OS内部的用户感知不到。通过客户端调用connect让OS来帮我们把三次握手的工作做完。 而accept是获取链接链接是已经建立好了的所以accept并不参与三次握手的任何细节accept一定是在获取链接前别人把链接做完既链接建立完。三次握手是OS自己完成的connect只是发起accept只是收尾。即使上层不调用accept,三次握手也是能够建立好的。 TCP保证可靠性不是write和read有关系的由双方OS完成的后面详谈。 建立链接后面就要断开链接所以UDP由于不需要建立链接自然不需要谈论断开链接 而四次挥手的工作都是由双方的OS完成而我们决定什么时候分手一旦调用系统调用close用户层就不用管了。 理解链接 谈男女朋友时都会表达自己的爱意一定有一方主动发起链接无论如何表达双方看对眼的概率是极低的。而主动发起链接是怎么发起的呢首先男方先表白然后女方在做表态什么时候在一起男方回答就现在。这就是双方三次握手成功。虽然现实生活中被拒绝是常态 建立链接究竟在干什么记下一些东西 什么是建立链接 所谓的建立链接三次握手根本就是手段不是目的为了达到让双方都能记住这一套一个服务端链接客户端很多客户端来链接了意味着很多的客户端来了OS应该区分清楚需要把链接管理起来先描述在组织需要创建对应的链接数据结构把所有的链接描述起来在对其进行管理。所谓的链接就是OS内部创建的链接结构体包含了在建立链接时对应的属性信息。当有新的链接进来时每到来一个链接服务端会构建一个链接对象 将所有的链接对象在内部中用特定的数据结构管理起来。这就是链接的建模过程。维护链接是需要成本的。占用内存资源要用对象进行管理。 断开链接需要四次挥手断开链接的最终目的毫无疑问就是把建立好的链接信息释放。四次挥手理解 男女朋友处的非常好走到了婚姻的殿堂但是被现实打败了过不下去啦。然后一方提出离婚但是你自己说了不算另一方说好啊过了一会对象又说离就离那我也要离那么你一看我也OK。所以断开链接是双方的事情必须得征求双方的意见。双方在协商TCP要保证可靠性你说的话要保证你也听到了我也知道了反之也一样。这就是传说中的四次挥手 TCP与UDP对比 可靠传输VS不可靠传输 有连接VS无连接 字节流VS数据报 定制协议 应用层协议的定制 再谈协议 协议是一种约定socket api的接口在读写数据时都是按照字符串的方式来接收的如果要传输一些”结构化的数据“怎么办呢 结构化的数据群里说话的时候除了消息本身还有头像昵称时间等等信息 。但是不是一个一个独立的个体你需要做的把这些消息形成一个报文——打包成一个字符串。 由多变一这个过程就是序列化。经过网络传输后收到的是一个报文收到一个报文要的是什么把一个字符串变成多个字符串这个过程是反序列化 业务数据发送到网络的时候先序列化发送收到的是序列字节流要先进行反序列化 然后才能使用 业务协议就是结构体这样说还是不够的所以我们要手写一个协议。 应用场景形成字符串对方收到收到之后上层来不及接收对方又发一个有可能一次全读的上层如何保证收到的是一个报文 tcp这里怎么保证收到一个完整的报文 理解业务协议理解序列化和反序列化。 网络版本计算器 例如, 我们需要实现一个服务器版的加法器. 我们需要客户端把要计算的两个加减乘除数发过去, 然后由服务器进行计算, 最后再把结果返回给客户端 TCP是面向字节流的所以明确报文和报文的边界 TCP是全双工的如果接收方来不及读那接收缓冲区就会存在很多数据读的时候怎么怎么保证读到一个完整的报文 1.定长2.特殊符号3.自描述方式 序列化、反序列化与定制协议是两码事是不同阶段的事情定制协议报头有效载荷 Protocal.hpp 自定义协议 #define SEP #define SEP_LEN strlen(SEP) #define LINE_SEP \r\n #define LINE_SEP_LEN strlen(LINE_SEP)请求和响应:Request,Response Request:x,y,op(“x op y”)x和y是数据op是操作符比如12 Response:设置了退出码exitcode和结果result() 对请求和响应添加报头,这里设置的报头是长度enLength即添加大小转化成字符串也就是封装了enLength函数 //x op y-content_len\r\nx op y\r\n //exitcode result-cotent_len\r\nexitcode result\r\n const std::string enLength(const std::string text) {std::string send_string std::to_string(text.size());send_string LINE_SEP;send_string text;send_string LINE_SEP;return send_string; }对请求和响应提取报文只要报文不要报头也就是封装了deLength函数 //cotent_len\r\nexitcode result\r\n bool deLength(const std::string package, std::string *text) {auto pos package.find(LINE_SEP);if (pos std::string::npos)return false;std::string text_len_string package.substr(0, pos);int text_len std::stoi(text_len_string);*text package.substr(pos LINE_SEP_LEN, text_len);return true; }对请求和响应进行序列化和反序列化对于序列化和反序列化我们可以用Json来进行实现 序列化过程结构化数据-“x op y” 反序列化过程:“x op y”-结构化数据 Protocal.hpp还提供了recvPackage函数 #define SEP #define SEP_LEN strlen(SEP) #define LINE_SEP \r\n #define LINE_SEP_LEN strlen(LINE_SEP) enum {OK 0,DIV_ZERO,MOD_ZERO,OP_ERROR };//x op y ---content_len\r\nx op y\r\n,添加报头 std::string enLength(const std::string text) {std::string send_string std::to_string(text.size());send_string LINE_SEP;send_string text;send_string LINE_SEP;return send_string; }//content_len\r\nexitcode result\r\n // 去掉报头得到exitcode result bool deLength(const std::string package, std::string *text) {auto pos package.find(LINE_SEP);if (pos std::string::npos)return false;std::string text_len_string package.substr(0, pos); // content_len:如“14”int text_len std::stoi(text_len_string);*text package.substr(pos LINE_SEP_LEN, text_len);return true; }class Request { public:Request() : x(0), y(0), op(0){}Request(int x_, int y_, char op_) : x(x_), y(y_), op(op_){}// 序列化// 结构化- x op ybool serialize(std::string *out){ #ifdef MYSELF*out ;std::string x_string std::to_string(x);std::string y_string std::to_string(y);*out x_string;*out SEP;*out op;*out SEP;*out y_string; #elseJson::Value root;root[first] x;root[second] y;root[oper] op;Json::FastWriter writer;*out writer.write(root); #endifreturn true;}// 反序列化化//x op y-结构化bool deserialize(const std::string in){ #ifdef MYSELFauto left in.find(SEP);auto right in.rfind(SEP);if (left std::string::npos || right std::string::npos)return false;if (left right)return false;if (right - (left SEP_LEN) ! 1)return false;std::string x_string in.substr(0, left);std::string y_string in.substr(right SEP_LEN);if (x_string.empty())return false;if (y_string.empty())return false;x std::stoi(x_string);y std::stoi(y_string);op in[left SEP_LEN]; #elseJson::Value root;Json::Reader reader;reader.parse(in, root);x root[first].asInt();y root[second].asInt();op root[oper].asInt(); #endifreturn true;}public:int x;int y;char op; };class Response { public:Response() : exitcode(0), result(0){}Response(int exitcode_, int result_) : exitcode(exitcode_), result(result_){}bool serialize(std::string *out){ #ifdef MYSELF*out ;std::string ec_string std::to_string(exitcode);std::string res_string std::to_string(result);*out ec_string;*out SEP;*out res_string; #elseJson::Value root;root[exitcode] exitcode;root[result] result;Json::FastWriter writer;*out writer.write(root); #endifreturn true;}bool deserialize(const std::string in){ #ifdef MYSELFauto mid in.find(SEP);if (mid std::string::npos)return false;std::string ec_string in.substr(0, mid);std::string res_string in.substr(mid SEP_LEN);if (ec_string.empty() || res_string.empty())return false;exitcode std::stoi(ec_string);result std::stoi(res_string); #elseJson::Value root;Json::Reader reader;reader.parse(in, root);exitcode root[exitcode].asInt();result root[result].asInt(); #endifreturn true;}public:int exitcode;int result; };// 读取报文,保证读取的是一个完整的报文 ,inbuffer由外部传入 // content_len\r\nx op y\r\ncontent_len\r\nx op y\r\ncontent_len\r\nx op bool recvPackage(int sock, std::string inbuffer, std::string *text) {char buffer[1024];while(true){ssize_t n recv(sock, buffer, sizeof(buffer) - 1, 0);if(n0){buffer[n] 0;inbufferbuffer;auto pos inbuffer.find(LINE_SEP);if(pos std::string::npos) continue;std::string text_len_string inbuffer.substr(0,pos);int text_len std::stoi(text_len_string);int total_len text_len_string.size()2*LINE_SEP_LENtext_len;std::cout处理前#inbuffer:\ninbufferstd::endl;if(inbuffer.size() total_len){std::cout你输入的消息,没有遵守所定制的协议,正在等待后续的内容,continuestd::endl;continue;}//至少是一个完整的报文*text inbuffer.substr(0,total_len);inbuffer.erase(0,total_len);std::cout处理后#inbuffer:\ninbufferstd::endl;break;}elsereturn false;}return true; }对于recvPackage函数我们要保证读到的至少是一个完整的报文 CalServer 服务端代码 //CalServer.hpp namespace server {enum{USAGE_ERR 1,SOCKET_ERR,BIND_ERR,LISTEN_ERR};static const uint16_t gport 8080;static const int gbacklog 5;typedef std::functionbool(const Request req, Response resp) func_t;void handlerEntery(int sock,func_t func){std::string inbuffer;while(true){//1.读取:content_len\r\nx op y\r\n//保证读到的消息是一个完整的请求std::string req_text,req_str;if(!recvPackage(sock,inbuffer,req_text)) return;std::cout带报头的请求:\nreq_textstd::endl;//去掉报头if(!deLength(req_text,req_str)) return;std::cout去掉报头的正文:\nreq_strstd::endl;//2.对请求Request,反序列化//2.1得到一个结构化的请求对象Request req;if(!req.deserialize(req_str)) return;//3.计算机处理————业务逻辑Response resp;func(req,resp);//4.对响应Response,进行序列化//4.1得到一个字符串std::string resp_str;resp.serialize(resp_str);std::cout计算完成序列化响应:resp_strstd::endl;//5.发送响应std::string send_string enLength(resp_str);std::cout构建完成完整的响应\nsend_stringstd::endl;send(sock,send_string.c_str(),send_string.size(),0);}}class CalServer{public:CalServer(const uint16_tport gport):_listensock(-1),_port(port){}void initServer(){_listensock socket(AF_INET,SOCK_STREAM,0);if(_listensock0){logMessage(FATAL,create socket error);exit(SOCKET_ERR);}logMessage(NORMAL,create socket success:%d,_listensock);struct sockaddr_in local;memset(local,0,sizeof(local));local.sin_family AF_INET;local.sin_port htons(_port);local.sin_addr.s_addr INADDR_ANY;if(bind(_listensock,(struct sockaddr*)local,sizeof(local))0){logMessage(FATAL,bind socket error);exit(BIND_ERR);}logMessage(NORMAL,bind socket success);if(listen(_listensock,gbacklog)0){logMessage(FATAL,listen socker error);exit(LISTEN_ERR);}logMessage(NORMAL,listen socket success);}void start(func_t func){for(;;){struct sockaddr_in peer;socklen_t len sizeof(peer);int sock accept(_listensock,(struct sockaddr*)peer,len);if(sock0){logMessage(ERROR,accept error,next);continue;}logMessage(NORMAL,accept a new link success,get new sock:%d,sock);pid_t id fork();if(id 0){close(_listensock);handlerEntery(sock,func);close(sock);exit(0);}close(sock);pid_t ret waitpid(id,nullptr,0);if(ret0){logMessage(NORMAL,wait child success);}}}~CalServer() {}public:int _listensock;uint16_t _port;}; }//CalServer.cc #include calServer.hpp #include memory using namespace server; using namespace std; static void Usage(string proc) {cout \nUsage:\n\t proc local_port\n\n; }// req是处理好的完整的请求对象 // resp:根据req进行业务处理填充resp不需要管理任何IO序列化和反序列化 bool cal(const Request req, Response resp) {// req已经有结构化的数据resp.exitcode OK;resp.result 0;switch (req.op){case :resp.result req.xreq.y;break;case -:resp.result req.x-req.y;break;case *:resp.result req.x*req.y;break;case /:{if(req.y0) resp.exitcode DIV_ZERO;else resp.result req.x/req.y;}break;case %:{if(req.y 0)resp.exitcode MOD_ZERO;else resp.result req.x%req.y;}break;default:resp.exitcode OP_ERROR;break;}return true; }// tcp服务器启动上和udp server一模一样 // ./tcpserver local_port int main(int argc, char *argv[]) {if (argc ! 2){Usage(argv[0]);exit(USAGE_ERR);}uint16_t port atoi(argv[1]);unique_ptrCalServer tsvr(new CalServer(port));tsvr-initServer();tsvr-start(cal);return 0; }CalClient ParseLine:解析构建一个请求“11” #pragma once #include iostream #include string #include sys/types.h #include sys/socket.h #include netinet/in.h #include arpa/inet.h #include unistd.h #include Protocol.hpp #define NUM 1024class CalClient { public:CalClient(const std::string serverip, const uint16_t serverport): _sock(-1), _serverip(serverip), _serverport(serverport){}void initClient(){// 1. 创建socket_sock socket(AF_INET, SOCK_STREAM, 0);if (_sock 0){std::cerr socket create error std::endl;exit(2);}}void start(){struct sockaddr_in server;memset(server, 0, sizeof(server));server.sin_family AF_INET;server.sin_port htons(_serverport);server.sin_addr.s_addr inet_addr(_serverip.c_str());if (connect(_sock, (struct sockaddr *)server, sizeof(server)) ! 0){std::cerr socket connect error std::endl;}else{std::string line;std::string inbuffer;while (true){std::cout mycal ;std::getline(std::cin, line); // 11Request req ParseLine(line); // 11std::string content;req.serialize(content);std::string send_string enLength(content);send(_sock, send_string.c_str(), send_string.size(), 0); // bug?? 不管std::string package, text;// content_len\r\nexitcode result\r\nif (!recvPackage(_sock, inbuffer, package))continue;if (!deLength(package, text))continue;// exitcode resultResponse resp;resp.deserialize(text);std::cout exitCode: resp.exitcode std::endl;std::cout result: resp.result std::endl;}}}Request ParseLine(const std::string line){// 建议版本的状态机//11 123*456 12/0int status 0; // 0:操作符之前1:碰到了操作符 2:操作符之后int i 0;int cnt line.size();std::string left, right;char op;while (i cnt){switch (status){case 0:{if(!isdigit(line[i])){op line[i];status 1;}else left.push_back(line[i]);}break;case 1:i;status 2;break;case 2:right.push_back(line[i]);break;}}std::cout std::stoi(left) std::stoi(right) op std::endl;return Request(std::stoi(left), std::stoi(right), op);}~CalClient(){if (_sock 0)close(_sock);}private:int _sock;std::string _serverip;uint16_t _serverport; };#include calClient.hpp #include memoryusing namespace std; static void Usage(string proc) {cout\nUasge:\n\tproc serverip serverport\n\n; } int main(int argc,char*argv[]) {if(argc!3){Usage(argv[0]);exit(1);}string serverip argv[1];uint16_t serverport atoi(argv[2]);unique_ptrCalClient tcli(new CalClient(serverip,serverport));tcli-initClient();tcli-start();return 0; }Json的安装 sudo yum install -y jsoncpp-devel
文章转载自:
http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn
http://www.morning.hdnd.cn.gov.cn.hdnd.cn
http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn
http://www.morning.cbmqq.cn.gov.cn.cbmqq.cn
http://www.morning.gkgb.cn.gov.cn.gkgb.cn
http://www.morning.kynf.cn.gov.cn.kynf.cn
http://www.morning.bflws.cn.gov.cn.bflws.cn
http://www.morning.hrpbq.cn.gov.cn.hrpbq.cn
http://www.morning.trbxt.cn.gov.cn.trbxt.cn
http://www.morning.sooong.com.gov.cn.sooong.com
http://www.morning.tsnmt.cn.gov.cn.tsnmt.cn
http://www.morning.ngmjn.cn.gov.cn.ngmjn.cn
http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn
http://www.morning.lqchz.cn.gov.cn.lqchz.cn
http://www.morning.skrrq.cn.gov.cn.skrrq.cn
http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn
http://www.morning.tfwg.cn.gov.cn.tfwg.cn
http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn
http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn
http://www.morning.nqbs.cn.gov.cn.nqbs.cn
http://www.morning.qwlml.cn.gov.cn.qwlml.cn
http://www.morning.wrbx.cn.gov.cn.wrbx.cn
http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn
http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn
http://www.morning.fpjw.cn.gov.cn.fpjw.cn
http://www.morning.jhrqn.cn.gov.cn.jhrqn.cn
http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn
http://www.morning.dhmll.cn.gov.cn.dhmll.cn
http://www.morning.slpcl.cn.gov.cn.slpcl.cn
http://www.morning.fksxs.cn.gov.cn.fksxs.cn
http://www.morning.zympx.cn.gov.cn.zympx.cn
http://www.morning.yaqi6.com.gov.cn.yaqi6.com
http://www.morning.mjzgg.cn.gov.cn.mjzgg.cn
http://www.morning.rrwft.cn.gov.cn.rrwft.cn
http://www.morning.zlbjx.cn.gov.cn.zlbjx.cn
http://www.morning.yrdt.cn.gov.cn.yrdt.cn
http://www.morning.syznh.cn.gov.cn.syznh.cn
http://www.morning.wyjhq.cn.gov.cn.wyjhq.cn
http://www.morning.rkzk.cn.gov.cn.rkzk.cn
http://www.morning.mplld.cn.gov.cn.mplld.cn
http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn
http://www.morning.kfclh.cn.gov.cn.kfclh.cn
http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn
http://www.morning.ytfr.cn.gov.cn.ytfr.cn
http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn
http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn
http://www.morning.cokcb.cn.gov.cn.cokcb.cn
http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn
http://www.morning.trrrm.cn.gov.cn.trrrm.cn
http://www.morning.wyppp.cn.gov.cn.wyppp.cn
http://www.morning.nzmw.cn.gov.cn.nzmw.cn
http://www.morning.fhghy.cn.gov.cn.fhghy.cn
http://www.morning.bdsyu.cn.gov.cn.bdsyu.cn
http://www.morning.ckrnq.cn.gov.cn.ckrnq.cn
http://www.morning.smxrx.cn.gov.cn.smxrx.cn
http://www.morning.skbhl.cn.gov.cn.skbhl.cn
http://www.morning.srbfz.cn.gov.cn.srbfz.cn
http://www.morning.mfjfh.cn.gov.cn.mfjfh.cn
http://www.morning.kjcll.cn.gov.cn.kjcll.cn
http://www.morning.ljzqb.cn.gov.cn.ljzqb.cn
http://www.morning.qhkx.cn.gov.cn.qhkx.cn
http://www.morning.csnch.cn.gov.cn.csnch.cn
http://www.morning.rfpb.cn.gov.cn.rfpb.cn
http://www.morning.kzpy.cn.gov.cn.kzpy.cn
http://www.morning.bsqbg.cn.gov.cn.bsqbg.cn
http://www.morning.bmtyn.cn.gov.cn.bmtyn.cn
http://www.morning.wpydf.cn.gov.cn.wpydf.cn
http://www.morning.srcth.cn.gov.cn.srcth.cn
http://www.morning.prjty.cn.gov.cn.prjty.cn
http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn
http://www.morning.dljujia.com.gov.cn.dljujia.com
http://www.morning.qdlr.cn.gov.cn.qdlr.cn
http://www.morning.jhxtm.cn.gov.cn.jhxtm.cn
http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn
http://www.morning.wmyqw.com.gov.cn.wmyqw.com
http://www.morning.sqmbb.cn.gov.cn.sqmbb.cn
http://www.morning.cffwm.cn.gov.cn.cffwm.cn
http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn
http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn
http://www.morning.tsxg.cn.gov.cn.tsxg.cn
http://www.tj-hxxt.cn/news/251682.html

相关文章:

  • 团购网站 seo大连做网站优化
  • wordpress文章编辑器路径seo是什么意思
  • 简洁的网站如何做网站百科
  • 微信手机官方网站wordpress 后台主题
  • wordpress付费插件网站知名电子商务网站有哪些
  • 设计师培训大纲宁波seo超级外链工具
  • 番禺制作网站系统网页游戏网站大全突袭
  • 自己学习建设网站江苏建设教育培训网
  • 用手机怎样制作网站浪味仙儿 wordpress
  • 自己做的网站怎么改背景图高级感ppt模板免费
  • 如何寻找建设网站的公司做网站推广需要做什么
  • 企业网站建设需要提供什么内容天津网站制作价格
  • 网站免费正能量软件下载视频适合一人开店的加盟店
  • 海南定安建设局网站医药公司网站建设方案
  • 怎么制作网站的链接宏网站备案域名购买
  • 网站开发开发需求文档无锡谁做网站好
  • 做暖暖免费网站软广告经典例子
  • 可以免费做调查问卷的网站怎么在百度上建网站
  • 门户网站名词解释做网站图结构
  • 大家都在哪些网站做宣传网站建设论文摘要
  • 杭州市建设工程质量安全监督总站网站可以查企业信息的软件
  • 好的网站怎么设计网站支付界面怎么做
  • 用wordpress建站案例建筑工程公司注册需要什么条件
  • 购买网站空间ftp设计徐州亿网
  • 榆林做网站需要注意的几点网站树状型结构优化
  • 外贸网站建设网站长春二手房
  • 重庆建设工程施工安全网优化大师使用方法
  • 有什么网站可以做编程题什么值得买wordpress模板
  • 公司网站建设wordpress目录怎么制作
  • 高端品牌建站广告设计工资高吗