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

百度上公司做网站wordpress整合论坛程序

百度上公司做网站,wordpress整合论坛程序,互联网销售可以卖什么产品,网站营销公司哪家好#x1f4bb;文章目录 前言ICMP概念报文格式 Ping服务实现系统调用函数具体实现运行测试 总结 前言 ping命令#xff0c;因为其简单、易用等特点#xff0c;几乎所有的操作系统都内置了一个ping命令。如果你是一名C初学者#xff0c;对网络编程、系统编程有所了解#xff… 文章目录 前言ICMP概念报文格式 Ping服务实现系统调用函数具体实现运行测试 总结 前言 ping命令因为其简单、易用等特点几乎所有的操作系统都内置了一个ping命令。如果你是一名C初学者对网络编程、系统编程有所了解但又没有多少实操经验的话不妨来尝试动手实现一个属于自己的ping命令。这样一来也能提高你对系统编程、网络编程的能力。 ICMP 概念 ICMP是工作在网络层的一种不可靠的传输协议意在辅助IP协议获取报文传输与网络连接的情况被广泛运用于网络诊断工具如ping 和 traceroute。 ICMP协议可以控制路由将报文错误原因返回给源主机从而实现对网络状况的诊断。 报文格式 ICMP协议被封装在IP协议之中以下为ICMP的报文固定格式 类型用于标识报文的类型ICMP报文类型分为两类信息类报文、差错类报文。 代码用于标识差错类报文的具体错误信息。 校验和用于计算报文是否出现损坏发送方填写接收方校验。 「ICMP常见消息类型」 ICMP 类型描述0回显应答Echo Reply对回显请求的响应通常用于ping操作。3目的不可达Destination Unreachable目标地址无法到达时发送包括网络不可达、主机不可达等子类型。4源抑制Source Quench请求发送方降低发送速率以防止网络拥塞现已弃用。5重定向Redirect建议主机将数据包发送到不同的路由器提供更优路径。8回显请求Echo Request请求目标主机返回应答消息通常用于ping操作。11超时Time Exceeded数据包在网络中传输时间超过TTL值或在分片重组过程中超时。12参数问题Parameter Problem数据包的IP头部存在错误导致无法处理。 「Linux中的实现」 Linux中ICMP报文格式有不少成员但只是实现ping服务只需要以下成员 icmp_typeicmp报文的类型。 icmp_cksum校验和用于计算数据是否损坏。 icmp_id用于标识报文的唯一性。 icmp_seq序列号字段多用于echo、echoreply功能。 icmp_data报文的内容只有8bit大小 「Linux中ICMP报文的描述」 /*Linux中icmp的有较多成员变量嫌麻烦可以看#define部分来认识主要成员变量*/ struct icmp {uint8_t icmp_type; /* icmp类型; type of message, see below */uint8_t icmp_code; /* type sub code */uint16_t icmp_cksum; /*校验和用于确定报文是否完整无损*/union{unsigned char ih_pptr; /* ICMP_PARAMPROB */struct in_addr ih_gwaddr; /* gateway address */struct ih_idseq /* echo datagram */{uint16_t icd_id;uint16_t icd_seq;} ih_idseq;uint32_t ih_void;/* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */struct ih_pmtu{uint16_t ipm_void;uint16_t ipm_nextmtu;} ih_pmtu;struct ih_rtradv{uint8_t irt_num_addrs;uint8_t irt_wpa;uint16_t irt_lifetime;} ih_rtradv;} icmp_hun; #define icmp_pptr icmp_hun.ih_pptr #define icmp_gwaddr icmp_hun.ih_gwaddr #define icmp_id icmp_hun.ih_idseq.icd_id #define icmp_seq icmp_hun.ih_idseq.icd_seq #define icmp_void icmp_hun.ih_void #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs #define icmp_wpa icmp_hun.ih_rtradv.irt_wpa #define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetimeunion{struct //存储时间戳{uint32_t its_otime; // 原始时间戳发送时的时间uint32_t its_rtime; // 接受时间戳接受时的时间uint32_t its_ttime; // 传输时间戳传输所用时间} id_ts;struct{struct ip idi_ip;/* options and then 64 bits of data */} id_ip;struct icmp_ra_addr id_radv;uint32_t id_mask;uint8_t id_data[1];} icmp_dun; #define icmp_otime icmp_dun.id_ts.its_otime #define icmp_rtime icmp_dun.id_ts.its_rtime #define icmp_ttime icmp_dun.id_ts.its_ttime #define icmp_ip icmp_dun.id_ip.idi_ip #define icmp_radv icmp_dun.id_radv #define icmp_mask icmp_dun.id_mask #define icmp_data icmp_dun.id_data };Ping服务实现 系统调用函数 原始套接字 要使用ICMP协议就必须绕过传输层(TCP/UDP)直接操作网络层所以必须使用原始套接字在Mac、Linux中使用原始套接字可能会需要root权限。 //函数原型 int socket(int domain, int type, int protocol);int _sockfd socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); //使用原始套接字信号转换 在Linux中的ping服务一般通过ctlc来实现终止所以得要将信号执行函数替换成自己的函数。 //函数原型 void (*signal(int sig, void (*func)(int)))(int);//使用方式 signal(SIGINT, [](int sig) {printf(sig:%d, sig); } );「域名转换为IP地址」 在Linux中将域名转成ip地址的函数有gethostbyname但其在新版本的linux中已经被废弃所以这里使用较新的getaddrinfo。 /*通过getaddrinfo获取的数据将存进该结构体*/ struct addrinfo {int ai_flags;int ai_family; //协议族int ai_socktype;int ai_protocol;socklen_t ai_addrlen; // sockaddr 的长度struct sockaddr *ai_addr; // 根据需求转换成sockaddr_inchar *ai_canonname;struct addrinfo *ai_next; //下一个addrinfo使用链表来连接匹配的IP。 };int getaddrinfo(const char *restrict node, //需要转换的域名const char *restrict service, //DNS服务器地址可为空const struct addrinfo *restrict hints, //用于限定获取的数据struct addrinfo **restrict res); //结果存放的指针具体实现 ping服务的实现使用了类来进行封装从而使得其更简洁易懂。 头文件声明 #include netdb.h #include sys/socket.h #include netinet/in.h #include arpa/inet.h #include signal.h #include unistd.h #include stdlib.h #include string.h #include netinet/ip_icmp.h #include string #include iostream #include format #include threadclass PingServer { public:PingServer(const char* ip); void Start(); static void TimeEnd(); // ping计算总结ctrlc调用。private:void Init(); // 初始化类void SendData(); //发送数据void RecvData(); //接受数据unsigned short CheckSum(void* data, int len); //计算校验和private:static std::chrono::system_clock::time_point _oldTime; //计算ping服务运行时间static int _sendSeq; //发送数据次数static int _recvSeq; //接受数据次数struct sockaddr_in _destAddr; //远端地址信息const char* _ip; //需要ping的ip/hostname;char _recvData[1024]; //接受数据缓冲区int _sockfd; //套接字unsigned short _id; //用于标识ip报文唯一性。 };//初始化静态成员 std::chrono::system_clock::time_point PingServer::_oldTime std::chrono::system_clock::now(); int PingServer::_sendSeq 0; int PingServer::_recvSeq 0;介绍完类的成员也该到其实现了⬇️。 #include netdb.h #include sys/socket.h #include netinet/in.h #include arpa/inet.h #include signal.h #include unistd.h #include stdlib.h #include assert.h #include stdio.h #include string.h #include netinet/ip_icmp.h #include string #include iostream #include format #include future #include thread//TODO chrono时钟实现超时class PingServer { public:PingServer(const char* ip):_ip(ip), _id(htons(getpid())){Init();}void Start(){std::thread(PingServer::SendData, this).detach();RecvData();}static void TimeEnd(){auto now std::chrono::system_clock::now();auto sum std::chrono::duration_caststd::chrono::milliseconds(now-_oldTime).count();int loss ((double)(_sendSeq - _recvSeq) / _sendSeq) * 100;std::cout std::format(\n{} packets transimitted, {} received, {}% packet loss, time {}ms, _sendSeq, _recvSeq, loss, sum) std::endl;}private:void Init(){_sockfd socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); //使用原始套接字if(_sockfd 0) {std::cerr socket error std::endl; exit(-1);}struct addrinfo hints{}, *res{}; hints.ai_family AF_INET; //限定获取IP为IPV4if(getaddrinfo(_ip, nullptr, hints, res) ! 0) //正确返回0{std::cerr hostname error std::endl;exit(EXIT_FAILURE);}sockaddr_in* ipv4 (sockaddr_in*)res-ai_addr; //转换成sockaddr_in结构 sockaddr-sockaddr_inmemcpy(_destAddr, ipv4, sizeof(sockaddr_in));}void SendData(){while (1){//装包struct icmp icmphdr{}; //需要发送的ICMP报文icmphdr.icmp_seq _sendSeq; icmphdr.icmp_type ICMP_ECHO; //ICMP报文的类型// icmphdr.icmp_type ICMP_TIMESTAMP; icmphdr.icmp_id _id; auto now std::chrono::system_clock::now(); // 获取时间戳, 8bitmemcpy(icmphdr.icmp_data, now, sizeof(now)); icmphdr.icmp_cksum CheckSum(icmphdr, sizeof(icmphdr)); // 计算校验和if(sendto(_sockfd, icmphdr, sizeof(icmphdr), 0, (struct sockaddr*)_destAddr, sizeof(_destAddr)) 0){ //发送数据std::cout send data fail _ip std::endl;exit(EXIT_FAILURE);}std::this_thread::sleep_for(std::chrono::seconds(1)); //每个一秒发送一次}}void RecvData(){while (1){sockaddr_in addr{};socklen_t fromLen sizeof(_destAddr);ssize_t n recvfrom(_sockfd, _recvData, sizeof(_recvData), 0, (sockaddr*)addr, fromLen);if(n 0){ struct ip* ip_hdr (struct ip*)_recvData; // 获取ICMP报文位置IP头部计算为首部字段长度*4;struct icmp* icmp_hdr (struct icmp*)(_recvData (ip_hdr-ip_hl 2)); if (icmp_hdr-icmp_type ICMP_ECHOREPLY icmp_hdr-icmp_id _id) //筛选{_recvSeq;//计算耗时auto now std::chrono::system_clock::now();auto data (std::chrono::system_clock::time_point*)icmp_hdr-icmp_data;auto sum std::chrono::duration_caststd::chrono::milliseconds(now - *data).count();std::cout std::format({} bytes from {}: icmp_seq{} ttl{} time{}ms,n, inet_ntoa(_destAddr.sin_addr), icmp_hdr-icmp_seq, ip_hdr-ip_ttl, sum) std::endl;}// else // {// std::cout std::format(icmp_type: {}, icmp_ip: {}, icmp_code: {}, icmp_hdr-icmp_type, icmp_hdr-icmp_id, icmp_hdr-icmp_code) std::endl;// }}else if(n 0){std::cerr Recv fail std::endl;exit(EXIT_FAILURE);}}}unsigned short CheckSum(void* data, int len){ unsigned short* buf (unsigned short*)data;unsigned sum 0;// 计算数据的和while(len 1){sum *buf;len - 2;}if(len 1){sum *(unsigned char*)buf;}// 把高16位和低16位相加sum (sum 16) (sum 0xffff);sum (sum 16);// 取反return (unsigned short)(~sum);}private:static std::chrono::system_clock::time_point _oldTime; static int _sendSeq;static int _recvSeq;unsigned short _id;int _sockfd;struct sockaddr_in _destAddr;const char* _ip; //需要ping的ip;char _recvData[1024]; };std::chrono::system_clock::time_point PingServer::_oldTime std::chrono::system_clock::now(); int PingServer::_sendSeq 0; int PingServer::_recvSeq 0;main函数 #include Ping.hpp//TOOD 初始化void Usage() {std::cout ping ip/hostname std::endl; }int main(int argc, char* argv[]) {if(argc ! 2){Usage();return 1;}signal(SIGINT, [](int sig) //当使用 ctlc 时中断程序。{PingServer::TimeEnd();exit(0);});PingServer ping(argv[1]);ping.Start();return 0; }运行测试 CMakeList cmake_minimum_required(VERSION 3.29) project(PingServer)set(CMAKE_CXX_STANDARD 20) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)add_executable(test test.cppPing.hpp )运行结果 总结 本篇文章实现了一个简易的ping指令其对系统编程、网络编程都有所涉及但真实的ping指令可远不止这么简单感兴趣的读者可以通过访问Linux开源项目来了解真正的实现。 博客主页主页 我的专栏C 我的githubgithub
文章转载自:
http://www.morning.nzkc.cn.gov.cn.nzkc.cn
http://www.morning.lskrg.cn.gov.cn.lskrg.cn
http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn
http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn
http://www.morning.slfmp.cn.gov.cn.slfmp.cn
http://www.morning.ssjry.cn.gov.cn.ssjry.cn
http://www.morning.yhljc.cn.gov.cn.yhljc.cn
http://www.morning.hkshy.cn.gov.cn.hkshy.cn
http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn
http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com
http://www.morning.jbtzx.cn.gov.cn.jbtzx.cn
http://www.morning.mntxalcb.com.gov.cn.mntxalcb.com
http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn
http://www.morning.ywpcs.cn.gov.cn.ywpcs.cn
http://www.morning.jcxzq.cn.gov.cn.jcxzq.cn
http://www.morning.tdcql.cn.gov.cn.tdcql.cn
http://www.morning.dhxnr.cn.gov.cn.dhxnr.cn
http://www.morning.fgqbx.cn.gov.cn.fgqbx.cn
http://www.morning.ccffs.cn.gov.cn.ccffs.cn
http://www.morning.klyyd.cn.gov.cn.klyyd.cn
http://www.morning.cykqg.cn.gov.cn.cykqg.cn
http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn
http://www.morning.rqfnl.cn.gov.cn.rqfnl.cn
http://www.morning.rcttz.cn.gov.cn.rcttz.cn
http://www.morning.yhplt.cn.gov.cn.yhplt.cn
http://www.morning.ssxlt.cn.gov.cn.ssxlt.cn
http://www.morning.yfcbf.cn.gov.cn.yfcbf.cn
http://www.morning.kndst.cn.gov.cn.kndst.cn
http://www.morning.fsrtm.cn.gov.cn.fsrtm.cn
http://www.morning.drrt.cn.gov.cn.drrt.cn
http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn
http://www.morning.cczrw.cn.gov.cn.cczrw.cn
http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn
http://www.morning.yzdth.cn.gov.cn.yzdth.cn
http://www.morning.hcgbm.cn.gov.cn.hcgbm.cn
http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com
http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn
http://www.morning.srgsb.cn.gov.cn.srgsb.cn
http://www.morning.lmknf.cn.gov.cn.lmknf.cn
http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn
http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn
http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn
http://www.morning.hphqy.cn.gov.cn.hphqy.cn
http://www.morning.mnslh.cn.gov.cn.mnslh.cn
http://www.morning.nqrdx.cn.gov.cn.nqrdx.cn
http://www.morning.qyqmj.cn.gov.cn.qyqmj.cn
http://www.morning.wcczg.cn.gov.cn.wcczg.cn
http://www.morning.qlry.cn.gov.cn.qlry.cn
http://www.morning.lbbrw.cn.gov.cn.lbbrw.cn
http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn
http://www.morning.bpknt.cn.gov.cn.bpknt.cn
http://www.morning.schwr.cn.gov.cn.schwr.cn
http://www.morning.pnmtk.cn.gov.cn.pnmtk.cn
http://www.morning.ryztl.cn.gov.cn.ryztl.cn
http://www.morning.hpdpp.cn.gov.cn.hpdpp.cn
http://www.morning.qdlr.cn.gov.cn.qdlr.cn
http://www.morning.hsrch.cn.gov.cn.hsrch.cn
http://www.morning.jfmyt.cn.gov.cn.jfmyt.cn
http://www.morning.cpqqf.cn.gov.cn.cpqqf.cn
http://www.morning.wyzby.cn.gov.cn.wyzby.cn
http://www.morning.zcsch.cn.gov.cn.zcsch.cn
http://www.morning.dqxph.cn.gov.cn.dqxph.cn
http://www.morning.wbysj.cn.gov.cn.wbysj.cn
http://www.morning.gmplp.cn.gov.cn.gmplp.cn
http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn
http://www.morning.dzdtj.cn.gov.cn.dzdtj.cn
http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn
http://www.morning.ndlww.cn.gov.cn.ndlww.cn
http://www.morning.clpkp.cn.gov.cn.clpkp.cn
http://www.morning.rpgdd.cn.gov.cn.rpgdd.cn
http://www.morning.pylpd.cn.gov.cn.pylpd.cn
http://www.morning.yxzfl.cn.gov.cn.yxzfl.cn
http://www.morning.hcxhz.cn.gov.cn.hcxhz.cn
http://www.morning.llllcc.com.gov.cn.llllcc.com
http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn
http://www.morning.nzdks.cn.gov.cn.nzdks.cn
http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn
http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn
http://www.morning.llcgz.cn.gov.cn.llcgz.cn
http://www.morning.ndltr.cn.gov.cn.ndltr.cn
http://www.tj-hxxt.cn/news/259626.html

相关文章:

  • 微网站开发协议浏览器下载安装
  • 太极馆如何做网站推广app网站
  • 论坛网站建设价格成都市做网站
  • 网站外链建设需要逐步进行适可优化即可在线定制logo
  • wordpress网站打开慢怎样做地方门户网站
  • 曲阜做网站哪家好wordpress+评论顺序
  • 四川省住房建设厅网站设计类专业考研
  • 视觉中国设计网站5118站长工具箱
  • 微博建网站推广比较好的网站有哪些
  • 如何做网站策划网站seo在线诊断
  • 网站规划思想方法有哪些内容wordpress 预约时间
  • 长沙 网站seo服务 网络服务网页搜索打不开网页
  • 为什么要做营销型的网站建设全球最新军事新闻
  • 昆明网站建设索王道下拉活动策划书模板
  • 某某网站建设策划书2000字金融公司网站方案
  • 网站开发vue版本是什么公司网址制作
  • 库尔勒网站建设公司重庆璧山网站制作公司推荐
  • 北京国税局网站官网入口用wordpress搭建知名网站
  • 杭州做网站哪家公司好设计公司企业文化
  • wordpress 商业网站php网站挂马
  • 淘宝网站建设的特点idc数据中心
  • 高度重视部门网站建设手机创建个人网站 免费
  • 微信公众号做微网站吗福建省建设干部培训中心网站首页
  • 企业多语言网站开发备案名 网站名
  • 查询网站到期时间阿里万网怎么做网站
  • 义乌网站建设托管平面图怎么画
  • 外贸网站营销方案中国菲律宾足球直播
  • 长沙网站设计报价wordpress 主机和域名绑定域名
  • 手机网站发布页电脑版网站滑块验证怎么做
  • 网站打开速度慢优化济南市规划局官网