{"id":124983,"date":"2020-06-27T03:57:34","date_gmt":"2020-06-26T19:57:34","guid":{"rendered":"http:\/\/4563.org\/?p=124983"},"modified":"2020-06-27T03:57:34","modified_gmt":"2020-06-26T19:57:34","slug":"%e6%88%91%e5%8f%88%e6%9d%a5%e4%ba%86%ef%bc%8c-%e5%8f%88%e6%98%af%e5%85%b3%e4%ba%8e-socket-%e7%9a%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=124983","title":{"rendered":"\u6211\u53c8\u6765\u4e86\uff0c \u53c8\u662f\u5173\u4e8e socket \u7684"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6211\u53c8\u6765\u4e86\uff0c \u53c8\u662f\u5173\u4e8e socket \u7684               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <span><i><\/i> 13<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>\u9644\u4e0a\u4ee3\u7801\uff1a<\/h1>\n<p> <\/p>\n<h1>server \u7aef<\/h1>\n<p> <\/p>\n<pre><code>from socketserver import BaseRequestHandler, ThreadingTCPServer   class Handler(BaseRequestHandler):      def setup(self) -&gt; None:         self.request.settimeout(1)      def handle(self):         while 1:             try:                 data = \"\u8fd9\u662f\u4e3b\u673a\u7aef\u7684\u4fe1\u606f\"                 self.request.sendall(data.encode(\"utf-8\"))             except BaseException as e:                 print(e)                 print(\"\u5173\u95ed\u8fde\u63a5:{}\".format(self.client_address[0]))                 break   if __name__ == '__main__':     host = (\"localhost\", 8000)     server = ThreadingTCPServer(host, Handler)     server.serve_forever() <\/code><\/pre>\n<h1>client \u7aef<\/h1>\n<p> <\/p>\n<pre><code>import socket import time   client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  host = (\"localhost\", 8000)  client.connect(host)   while 1:     data = client.recv(2000)     if len(data) &lt;= 0:         break      print(\"\u4ece\u4e3b\u673a\u7aef\u63a5\u53d7\u4fe1\u606f:{}\".format(data.decode()))     # time.sleep(1)  <\/code><\/pre>\n<h2>\u8fd0\u884c client \u7684\u65f6\u5019\u6709\u7684\u65f6\u5019\u4f1a\u62a5\u9519\uff0c<em>&#8216;utf-8&#8217; codec can&#8217;t decode bytes in position<\/em><\/h2>\n<h2>\u4f46\u662f\u5982\u679c\u5728 server \u7aef self.request.sendall \u53d1\u9001\u82f1\u6587\u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u62a5\u9519\uff0c \u4e2a\u4eba\u4f30\u8ba1\u662f\u56e0\u4e3a\u7f16\u7801\u540e\u7684\u4e2d\u6587\u4f20\u8f93\u7684\u65f6\u5019 client \u7aef\u63a5\u6536\u7684\u6570\u636e\u4e0d\u591f\u5b8c\u6574\uff0c \u6bd4\u5982\u4e00\u4e2a \u7f16\u8bd1\u540e\u7684 \u5b57\u8282\u6570\u636e\u662f <em>xe5x93x88<\/em> \u4f46\u662f\u63a5\u6536\u7684\u65f6\u5019\u53ea\u6709\u63a5\u6536\u5230 <em>xe5x9<\/em> \u5bfc\u81f4 client \u7aef\u89e3\u6790\u7684\u65f6\u5019\u51fa\u9519\u4e86\uff0c \u5927\u5bb6\u6709\u4ec0\u4e48\u65b9\u6cd5\u4fdd\u8bc1\u6570\u636e\u7684\u5b8c\u6574\u6027\u4e48<\/h2>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>15<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2099643\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd8\u6709\uff0c \u5173\u4e8e self.request.recv()\u7684 \u51fd\u6570\u600e\u4e48\u4e0d\u963b\u585e\uff0c <\/p>\n<p>\u6211\u5728 Handler \u4e2d\u7684 setup \u51fd\u6570\u4e2d\u8bbe\u7f6e self.request.settimeout(0) \u4e4b\u540e\u4f1a\u62a5\u4e00\u4e2a \u53c2\u8003\u7684\u5bf9\u8c61\u7c7b\u578b\u4e0d\u652f\u6301\u5c1d\u8bd5\u7684\u64cd\u4f5c\u9519\u8bef\uff0c \u5e94\u8be5\u600e\u4e48\u89e3\u51b3\u5462                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099644\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : est <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd8\u662f\u53bb\u5b66\u4e00\u4e0b python \u57fa\u7840\u5427\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099645\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CEBBCAT <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd8\u9700\u8981\u5b66\u4e60 TCP \u57fa\u7840\uff0c\u518d\u51fa\u9519\u4e5f\u4e0d\u81f3\u4e8e\u4f20\u534a\u4e2a\u5b57\u8282\u7ed9 Python \u5450\u2026\u2026<\/p>\n<p>\u53e6\u5916\u4f60\u8fd9\u662f\u9762\u5411\u8bba\u575b\u5de5\u4f5c\u5417\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099646\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Caratpine <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b66\u4e60 TCP +1\uff0cTCP \u662f\u6d41\u4f20\u8f93\u3002<br \/>\u4e00\u79cd\u529e\u6cd5\u662f\u5728\u4f20\u9001\u6d88\u606f\u7684\u65f6\u5019\u628a\u6d88\u606f\u957f\u5ea6\u4f5c\u4e3a\u5934\u4fe1\u606f\u4e5f\u4f20\u7ed9\u5ba2\u6237\u7aef\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099647\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b HTTP \u4e2d\u7684 chunk \u7f16\u7801\u65b9\u5f0f\u3002\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u5148\u53d1\u9001\u4e00\u4e2a\u957f\u5ea6\uff0c\u7136\u540e\u518d\u53d1\u5bf9\u5e94\u957f\u5ea6\u7684\u6570\u636e\u3002\u8fd9\u6837\u5ba2\u6237\u7aef\u5c31\u53bb\u53ef\u4ee5\u5224\u65ad\u6570\u636e\u662f\u5426\u5b8c\u6574\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099648\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyzj <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             data = client.recv(24)<br \/>\u8fd9\u6837\u5c31\u4e0d\u4f1a\u62a5\u9519\u4e86<br \/>\u54c8\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099649\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yeept <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Socket \u4e00\u5b9a\u8981\u6ce8\u610f\u5904\u7406\u6570\u636e\u8fb9\u754c\uff0c\u53ef\u53c2\u8003 #5 \u65b9\u5f0f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099650\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u7684\uff0c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099651\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mingl0280 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             TCP \u57fa\u7840\u5e38\u8bc6:TCP \u662f\u4e00\u4e2a\u5b57\u8282\u6d41\u534f\u8bae,\u5ba2\u6237\u7aef\u6216\u670d\u52a1\u5668\u6536\u5230\u7684\u5b57\u8282\u6d41\u6709\u591a\u957f\u6ca1\u4eba\u77e5\u9053.TCP \u53ea\u4fdd\u8bc1\u5b57\u8282\u6d41\u7684\u987a\u5e8f\u662f\u6b63\u786e\u7684.\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 TCP \u9700\u8981\u4e00\u4e2a\u4e0a\u5c42\u7684\u534f\u8bae\u6765\u4fdd\u8bc1\u6d88\u606f\u5b8c\u6574\u6027\u7684\u539f\u56e0.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099652\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cweijan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u6b63\u89e3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099653\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hankai17 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5b9e\u73b0\u4e00\u5957 http \u7528\u6765\u6536\u53d1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099654\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Chenamy2017 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u5b66\u4e60\u4e0b TCP \u6570\u636e\u6d41\u5904\u7406\u5427<br \/>2. \u8c03\u8bd5\u65f6\u4f60\u53ef\u4ee5\u5c06\u6536\u5230\u7684\u6570\u636e\u6253\u5370\u51fa\u6765\uff08\u7528\u5341\u516d\u8fdb\u5236\uff09\u770b\u770b\u4f60\u90fd\u6536\u5230\u4e86\u4e9b\u4ec0\u4e48\u6570\u636e\uff0c\u770b\u5230\u6570\u636e\u4f60\u5c31\u660e\u660e\u767d\u767d\u7684\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099655\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : gesse <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             client.recv(2000)<\/p>\n<p>\u4f60\u8fd9\u4e2a\u628a utf-8 \u7f16\u7801\u622a\u65ad\u4e86\u3002<\/p>\n<p>&gt;&gt;&gt; len(&#8216;\u8fd9\u662f\u4e3b\u673a\u7aef\u7684\u4fe1\u606f&#8217;.encode(&#8216;utf-8&#8217;))<br \/>24<\/p>\n<p>\u4f60\u628a 2000 \u6539\u6210 24 \u7684\u500d\u6570\u5c31\u53ef\u4ee5\u4e86\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099656\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @gesse \u4f60\u7684\u8fd9\u4e2a\u65b9\u6cd5\u7684\u786e\u6709\u7528\uff0c \u4f46\u662f\u5982\u679c\u670d\u52a1\u7aef\u53d1\u9001\u7684\u6570\u636e\u662f\u4e0d\u5b9a\u957f\u7684\u5462\uff1f\u4e0d\u662f\u6574\u500d\u6570                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2099657\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u660e\u767d\u4e86\u5404\u4f4d\u6240\u8bf4\u7684\uff0c \u6211\u7684\u539f\u610f\u662f \u6bd4\u5982\u8bf4 \u54c8 \u7684\u7f16\u7801\u662f b&#8217;xe5x93x88&#8242; \u4f46\u662f\u7531\u4e8e recv \u4e2d buff_size \u9650\u5236\u539f\u56e0\u5bfc\u81f4\u53ea\u53d6\u5230\u4e86\u524d\u4e24\u4e2a\u5b57\u8282 b&#8217;xe5x93&#8242; \u6240\u4ee5 decode \u7684\u65f6\u5019\u4f1a\u62a5\u9519\uff0c \u6211\u73b0\u5728\u77e5\u9053 xe5 \u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u8282\uff0c \u6240\u4ee5\u95ee\u7684\u65f6\u5019\u624d\u4f1a\u72af\u4e0a\u9762\u7684\u9519\u8bef\uff0c \u73b0\u5728\u662f\u9700\u8981\u5982\u4f55\u5c06\u5b8c\u6574\u7684\u6570\u636e\u62fc\u63a5\u8d77\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u53c8\u6765\u4e86\uff0c \u53c8\u662f\u5173\u4e8e socket&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124983"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=124983"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/124983\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=124983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=124983"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=124983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}