{"id":53921,"date":"2023-10-06T01:10:53","date_gmt":"2023-10-05T17:10:53","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=53921"},"modified":"2023-10-06T01:10:57","modified_gmt":"2023-10-05T17:10:57","slug":"xyjxjdm","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/53921.html","title":{"rendered":"654\u534f\u8bae\u89e3\u6790\u53ca\u4ee3\u7801"},"content":{"rendered":"\n
\u3000\u3000\u5728\u7f51\u7edc\u901a\u4fe1\u9886\u57df\u4e2d\uff0c\u534f\u8bae\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u534f\u8bae\u89c4\u5b9a\u4e86\u7f51\u7edc\u901a\u4fe1\u53cc\u65b9\u4e4b\u95f4\u7684\u901a\u4fe1\u89c4\u5219\uff0c\u4f7f\u5f97\u6570\u636e\u80fd\u591f\u51c6\u786e\u3001\u9ad8\u6548\u5730\u4f20\u8f93\u3002654\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f51\u7edc\u901a\u4fe1\u534f\u8bae\uff0c\u672c\u6587\u5c06\u6df1\u5165\u89e3\u6790654\u534f\u8bae\u7684\u539f\u7406\u548c\u76f8\u5173\u7684\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n\n\n\n
\u3000\u3000654\u534f\u8bae\u662f\u4e00\u79cd\u57fa\u4e8eTCP\/IP\u534f\u8bae\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u7528\u4e8e\u5b9e\u73b0\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u3002\u5b83\u7684\u540d\u79f0\u6765\u6e90\u4e8e\u534f\u8bae\u4e2d\u4f7f\u7528\u7684\u4e09\u4e2a\u5b57\u6bb5\uff1a\u8d77\u59cb\u7801\u3001\u957f\u5ea6\u548c\u6821\u9a8c\u7801\u3002654\u534f\u8bae\u7684\u8bbe\u8ba1\u76ee\u6807\u662f\u63d0\u4f9b\u9ad8\u6548\u3001\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\uff0c\u5e76\u4e14\u80fd\u591f\u9002\u5e94\u4e0d\u540c\u7684\u7f51\u7edc\u73af\u5883\u548c\u4f20\u8f93\u9700\u6c42\u3002<\/p>\n\n\n\n
\u3000\u3000\u5728654\u534f\u8bae\u4e2d\uff0c\u8d77\u59cb\u7801\u7528\u4e8e\u6807\u8bc6\u6570\u636e\u5305\u7684\u5f00\u59cb\u3002\u5b83\u901a\u5e38\u662f\u4e00\u4e2a\u56fa\u5b9a\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u7528\u4e8e\u544a\u8bc9\u63a5\u6536\u65b9\u6570\u636e\u5305\u7684\u5f00\u59cb\u4f4d\u7f6e\u3002\u8d77\u59cb\u7801\u7684\u957f\u5ea6\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u5b9a\u4e49\uff0c\u901a\u5e38\u4e3a\u51e0\u4e2a\u5b57\u8282\u3002<\/p>\n\n\n\n
\u3000\u3000\u957f\u5ea6\u5b57\u6bb5\u7528\u4e8e\u8bb0\u5f55\u6570\u636e\u5305\u7684\u957f\u5ea6\u3002\u5728654\u534f\u8bae\u4e2d\uff0c\u957f\u5ea6\u5b57\u6bb5\u901a\u5e38\u662f\u4e00\u4e2a\u56fa\u5b9a\u957f\u5ea6\u7684\u6574\u6570\uff0c\u7528\u4e8e\u544a\u8bc9\u63a5\u6536\u65b9\u6570\u636e\u5305\u7684\u957f\u5ea6\u3002\u63a5\u6536\u65b9\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u957f\u5ea6\u5b57\u6bb5\u6765\u786e\u5b9a\u6570\u636e\u5305\u7684\u957f\u5ea6\uff0c\u4ece\u800c\u6b63\u786e\u5730\u63a5\u6536\u6570\u636e\u3002<\/p>\n\n\n\n
\u3000\u3000\u6821\u9a8c\u7801\u7528\u4e8e\u9a8c\u8bc1\u6570\u636e\u5305\u7684\u5b8c\u6574\u6027\u3002\u5728654\u534f\u8bae\u4e2d\uff0c\u6821\u9a8c\u7801\u901a\u5e38\u662f\u901a\u8fc7\u5bf9\u6570\u636e\u5305\u4e2d\u7684\u6bcf\u4e2a\u5b57\u8282\u8fdb\u884c\u8ba1\u7b97\u5f97\u5230\u7684\u3002\u63a5\u6536\u65b9\u53ef\u4ee5\u901a\u8fc7\u5bf9\u63a5\u6536\u5230\u7684\u6570\u636e\u5305\u91cd\u65b0\u8ba1\u7b97\u6821\u9a8c\u7801\uff0c\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u4e0e\u63a5\u6536\u5230\u7684\u6821\u9a8c\u7801\u8fdb\u884c\u6bd4\u8f83\uff0c\u4ece\u800c\u5224\u65ad\u6570\u636e\u5305\u662f\u5426\u5b8c\u6574\u3002<\/p>\n\n\n\n
\u3000\u3000\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684654\u534f\u8bae\u7684\u4ee3\u7801\u5b9e\u73b0\u793a\u4f8b\uff1a<\/p>\n\n\n\n
\u3000\u3000“`python<\/p>\n\n\n\n
\u3000\u3000# \u5b9a\u4e49\u8d77\u59cb\u7801<\/p>\n\n\n\n
\u3000\u3000START_CODE = b'\\x55\\xAA'<\/p>\n\n\n\n
\u3000\u3000# \u5b9a\u4e49\u6570\u636e\u5305\u7c7b<\/p>\n\n\n\n
\u3000\u3000class Packet:<\/p>\n\n\n\n
\u3000\u3000def __init__(self, data):<\/p>\n\n\n\n
\u3000\u3000self.data = data<\/p>\n\n\n\n
\u3000\u3000def to_bytes(self):<\/p>\n\n\n\n
\u3000\u3000length = len(self.data)<\/p>\n\n\n\n
\u3000\u3000checksum = sum(self.data) % 256<\/p>\n\n\n\n
\u3000\u3000return START_CODE + length.to_bytes(2. ‘big') + self.data + checksum.to_bytes(1. ‘big')<\/p>\n\n\n\n
\u3000\u3000# \u53d1\u9001\u6570\u636e\u5305<\/p>\n\n\n\n
\u3000\u3000def send_packet(data):<\/p>\n\n\n\n
\u3000\u3000packet = Packet(data)<\/p>\n\n\n\n
\u3000\u3000# \u53d1\u9001\u6570\u636e\u5305\u7684\u903b\u8f91\u4ee3\u7801<\/p>\n\n\n\n
\u3000\u3000# \u63a5\u6536\u6570\u636e\u5305<\/p>\n\n\n\n
\u3000\u3000def receive_packet():<\/p>\n\n\n\n
\u3000\u3000# \u63a5\u6536\u6570\u636e\u5305\u7684\u903b\u8f91\u4ee3\u7801<\/p>\n\n\n\n
\u3000\u3000# \u89e3\u6790\u8d77\u59cb\u7801\u3001\u957f\u5ea6\u5b57\u6bb5\u3001\u6821\u9a8c\u7801\u7b49<\/p>\n\n\n\n
\u3000\u3000# \u4f7f\u7528\u793a\u4f8b<\/p>\n\n\n\n
\u3000\u3000data = b'Hello, World!'<\/p>\n\n\n\n
\u3000\u3000send_packet(data)<\/p>\n\n\n\n
\u3000\u3000received_data = receive_packet()<\/p>\n\n\n\n
\u3000\u3000“`<\/p>\n\n\n\n
\u3000\u3000\u4ee5\u4e0a\u4ee3\u7801\u662f\u4e00\u4e2a\u7b80\u5355\u7684654\u534f\u8bae\u7684\u5b9e\u73b0\u793a\u4f8b\u3002\u5728\u53d1\u9001\u6570\u636e\u5305\u65f6\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2aPacket\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528to_bytes\u65b9\u6cd5\u5c06\u6570\u636e\u5305\u8f6c\u6362\u4e3a\u5b57\u8282\u6d41\u3002\u5728\u63a5\u6536\u6570\u636e\u5305\u65f6\uff0c\u9700\u8981\u89e3\u6790\u8d77\u59cb\u7801\u3001\u957f\u5ea6\u5b57\u6bb5\u548c\u6821\u9a8c\u7801\u7b49\u4fe1\u606f\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u5904\u7406\u3002<\/p>\n\n\n\n
\u3000\u3000654\u534f\u8bae\u662f\u4e00\u79cd\u57fa\u4e8eTCP\/IP\u534f\u8bae\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u7528\u4e8e\u5b9e\u73b0\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u3002\u672c\u6587\u5bf9654\u534f\u8bae\u7684\u539f\u7406\u8fdb\u884c\u4e86\u8be6\u7ec6\u89e3\u6790\uff0c\u5e76\u7ed9\u51fa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4ee3\u7801\u5b9e\u73b0\u793a\u4f8b\u3002\u901a\u8fc7\u6df1\u5165\u7406\u89e3654\u534f\u8bae\u7684\u5de5\u4f5c\u539f\u7406\u548c\u4ee3\u7801\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u5e94\u7528\u548c\u4f18\u5316\u7f51\u7edc\u901a\u4fe1\u4e2d\u7684\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728\u7f51\u7edc\u901a\u4fe1\u9886\u57df\u4e2d\uff0c\u534f\u8bae\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u90e8\u5206\u3002\u534f\u8bae\u89c4\u5b9a\u4e86\u7f51\u7edc\u901a\u4fe1\u53cc\u65b9\u4e4b\u95f4\u7684\u901a\u4fe1\u89c4\u5219\uff0c\u4f7f\u5f97\u6570\u636e\u80fd\u591f\u51c6\u786e\u3001\u9ad8\u6548\u5730\u4f20\u8f93\u3002654\u534f\u8bae\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u7f51\u7edc\u901a\u4fe1\u534f\u8bae<\/p>\n","protected":false},"author":1,"featured_media":26938,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[137],"class_list":["post-53921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-txxy"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/53921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=53921"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/53921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/26938"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=53921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=53921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=53921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}